Issue Bug Under Review Quiet

#78961Extbase removing one file reference not working

Deleted Account 12/12/16, 17:49 updated 10/29/26, 12:26

Description

After setting a property pointing to a file reference to NULL and persisting, the file_reference still exists in the database and is back when refreshing the page.
(Even with the cascade remove@ annotation, the reference isn’t deleted.)

// Relevant SQL

CREATE TABLE tx_experiments_domain_model_book (

  -- ...

  cover_image int(11) DEFAULT '0' NOT NULL,

  -- ...

)

// Relevant TCA

// ...

'cover_image' => [
    'label' => 'Cover Image',
    'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('cover_image', [
        'maxitems' => 1,
        'minitems' => 0,
        'appearance' => [
            'createNewRelationLinkTitle' => 'LLL:EXT:fluid_styled_content/Resources/Private/Language/Database.xlf:tt_content.asset_references.addFileReference'
        ],
        'foreign_match_fields' => [
            'fieldname' => 'cover_image',
            'tablenames' => 'tx_experiments_domain_model_book',
            'table_local' => 'sys_file',
        ],
        'foreign_types' => $GLOBALS['TCA']['tt_content']['columns']['image']['config']['foreign_types']
    ], $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'])
],

// ...

// Model

class Book extends AbstractEntity
{
    // ...

    /**
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @cascade remove
     */
    protected $coverImage;

    // ...
}

// Controller

class BookController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    // ...

    protected function updateAction(Book $book)
    {
        $model->setCoverImage(null);

        $this->bookRepository->update($book);

        // ...
    }

    // ...
}

After taking a look at TYPO3\CMS\Extbase\Persistence\Generic\Backend it was clear to me why it does not work:
In the persistObject method there’s no clause to check for properties that have been changed from a DomainObject to null and have a @cascade remove annotation.

I have created a patch that would add the missing behaviour. (See Backend.patch)

Reviews 2

Files 1

History 613

  1. Gerrit Code Review pushed patch set 1 for master to change 52067 status New Under Review <span data-processor="relativetime" data-value="2017-03-16T16:35:10+00:00" > 03/16/17, 16:35 </span>

  2. Susanne Moog commented and changed status Under Review Accepted <span data-processor="relativetime" data-value="2019-03-01T19:23:08+00:00" > 03/01/19, 19:23 </span>

    Patch has been abandoned.

  3. Gerrit Code Review pushed patch set 1 for main to change 96722 status Accepted Under Review <span data-processor="relativetime" data-value="2026-10-15T13:58:26+00:00" > 10/15/26, 13:58 </span>

  4. Alexander Schnitzler commented and changed assigned to Alexander Schnitzler <span data-processor="relativetime" data-value="2026-10-16T07:24:42+00:00" > 10/16/26, 07:24 </span>

    I think the #[Cascade('remove')] in this report is misleading. According to the documentation, cascade remove controls what happens to related objects when the parent object is deleted. Here the parent (the book) is not deleted, only its cover image is set to null, so cascade remove should not matter.

    The case itself is still relevant, though. The bug is that setting a 1:1 relation stored via foreign_field to null has no effect, with or without cascade:

    $book->setCoverImage(null);
    $bookRepository->update($book);
    $persistenceManager->persistAll();

    The parent column is updated, but the sys_file_reference still points to the book via uid_foreign/@tablenames@/@fieldname@. As the relation is resolved via foreign_field, the reference is back on the next load (for a non-lazy property). It is also still shown in the backend.

    This is not specific to FAL: a plain inline child (type=inline, foreign_field, maxitems 1) behaves the same way.

    For comparison, FormEngine handles this by sending a delete command for the removed child, so in the backend a removed 1:1 child is always deleted.