I get a logical error but I don't know how the right way to correct it. Any help would be appreciate.
So, I've 2 entities User and Address. An address can not be created without a User.
I've a Many to One relationship (Many Addresses to One User).
When I try to register a User, I get this error :
An exception occurred while executing 'INSERT INTO ...
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'UserID' cannot be null
Logic because user doesn't exist. But is there a way to say to symfony save my user, get the user id and then persist the address ?
Address Entity
/**
* @ORM\ManyToOne(targetEntity="CTC\Bundle\UserBundle\Entity\User", inversedBy="addresses")
* @ORM\JoinColumn(name="UserID", referencedColumnName="id", nullable=false)
*/
private $user;
User Entity (I'm using FOSUser)
/**
* @ORM\OneToMany(targetEntity="CTC\Bundle\AddressBundle\Entity\Address", mappedBy="user", cascade={"persist"})
* @ORM\JoinColumn(name="AddressID", referencedColumnName="AddressID", nullable=false)
*/
private $addresses;
public function __construct()
{
parent::__construct();
$this->addresses = new ArrayCollection();
}
The registration type
...
$builder->add('addresses','collection', array(
'type'=>new AddressType(),
'allow_add' => true,
'allow_delete' => true
));
...
Aucun commentaire:
Enregistrer un commentaire