Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2: Error when saving a one-to-many relationship
    primarykey
    data
    text
    <p>I get this error: </p> <blockquote> <p>Integrity constraint violation: 1048 Column 'user_id' cannot be null </p> </blockquote> <p>When I try to save a User with an Address in my controller:</p> <pre><code> $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $user=new User(); $adr=new Address(); $user-&gt;getAddresses()-&gt;add($adr); /* i add something missing here */ $form =$this-&gt;createForm(new UserForm(),$user); if ('POST' == $request-&gt;getMethod()) { $form-&gt;bindRequest($request); if ($form-&gt;isValid()) { $em-&gt;persist($user); $em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('homepage')); } } </code></pre> <p>Well, these are my entities classes, User and Address: </p> <pre><code>public class User { /** * @var integer $id * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var Address * * @ORM\OneToMany(targetEntity="Address", mappedBy="user", cascade={"all"}) * @ORM\OrderBy({"zipCode" = "ASC"}) */ protected $addresses; } public class Address{ /** * @var User * * @ORM\ManyToOne(targetEntity="User",inversedBy="addresses") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="id") * }) */ private $user; } </code></pre> <p>in the form class:</p> <pre><code>$builder-&gt;add('addresses', 'collection', array('type' =&gt; new AddressForm())); </code></pre> <p>In the log, there's this message:</p> <pre><code>INSERT INTO user (email, password, salt, is_active, username, description, user_type_id,partner_type_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?){"1":"test@yahoo.ca","2":"ttttt","3":null,"4":null,"5":null,"6":"sdfadf","7":3,"8":9}) </code></pre> <p>And when it try to save the address:</p> <pre><code>INSERT INTO address (Number, voie_name, additional_address, zip_code, city, cedex, created_at, tel, loc_lat, loc_lng, voie_id, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ({"1":"12","2":"asdf","3":"sdfasdf","4":"69001","5":"Lyon 1er","6":null,"7":null,"8":"04 65 46 46 54","9":null,"10":null,"11":4,"12":null}) </code></pre> <p>Pay attention to <code>"12":null</code>, it's odd because Doctrine already saved the user.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload