Insert null in a relationship with the doctrine

1

I have a table of votes and a table of users, where for each vote entered the user can be identified (relationship one-to-many ).

If the user does not identify himself, how do I insert NULL into the foreign key of the user? I tried to create a new user object and perform the insert but could not.

The error always returned is as follows:

  

A new entity was found through the relationship 'Portal \ Model \ Voto # usuario' that was not configured to cascade persist operations for entity: Portal \ Model \ User @ 000000000199805b000000000453ebc2. To solve this issue: Either explicitly call EntityManager # persist () on this unknown entity or set cascade persist this association in the mapping for example @ManyToOne (.., cascade = {"persist"}). If you can not find out which entity causes the problem to implement 'Portal \ Model \ User #__ toString ()' to get a clue.

    
asked by anonymous 24.04.2015 / 16:18

2 answers

2

The problem in the specific case of your question is that you are not persisting the user before the vote persists. You should do something like this:

$entityManager->persist($user);
$vote->setUser($user);
$entityManager->persist($vote);
$entityManager->flush();

When allowing the user to be null, simply add the following annotation in the vote relationship (I believe ManyToOne ) to the user:

@ORM\JoinColumn(nullable=true)
    
27.04.2015 / 22:14
1

I believe the problem can be solved with the nullable=true setting in doctrine settings for your foreign key.

    
27.04.2015 / 19:46