Controlling database relations during repository actions

Hello,

I had a problem with my custom model’s repository.

I have a model with an avatar (Image), with a simple OneToOne relation:

/**
 * @var Image
 * @ORM\OneToOne
 * @ORM\Column(nullable=true)
 */
protected $avatar;

Now, imagine I have an instantance of this model which I want to get rid of its avatar, so I simply do

$myModel->setAvatar(null);
$this->myModelRepository->update($myModel);

I then get the following error:

An exception occurred while executing ‘INSERT INTO neos_flow_resourcemanagement_persistentresource (persistence_object_identifier, collectionname, filename, filesize, relativepublicationpath, mediatype, sha1, md5) VALUES (?, ?, ?, ?, ?, ?, ?, ?)’: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

This means that my repository is trying to insert null in place of an Image. How can I implicitly tell doctrine to not do that for Null values ? I’d expect this to be default behaviour, or is there another way to “break” a relation in such cases ? I tried to do unset($this->avatar) instead of using setAvatar without success either.

Now I’m updating an existing model with a change in its OneToOne property and it’s trying to INSERT the property instead of updating it, thus failing with duplicate PRIMARY KEY. So, isn’t Flow able to determine if it needs to update or insert properly when it comes to relations ? Is there a way to easily “guide” the framework on the action it needs to take, or I have to do it manually ?

Hi @dimitriadisg

One thing come to my mind. Since this is a OneToOne it means that the relation you add, will not exists without the relation. So you might have to look into adding something like cascade={"remove"}, orphanRemoval=true to the property annotation in order for Doctrine ORM to remove it properly

Maybe this is more fitting cascade={"persist", "remove"}