I've the following entities (School and Circuit) created in my symfony2/doctrine2 project with a many-To-One (A circuit contains one or more schools) bi-directional relationship. Here is the Schoool.orm.yml and circuit.orm.yml definition
I'm trying to load the corresponding data fixture for each of this entities as shown below:
//Circuit Fixture
public function load(ObjectManager $em) {
$newCirco = new Circonscription();
$newCirco->setId(1);
$newCirco->setLibelle('Circuit1');
$em->persist($newCirco);
$em->flush();
$this->addReference('circo1', $newCirco);
}
//School Fixture
public function load(ObjectManager $em) {
//Ecoles - Circo 1
$ecole = new Etablissement();
$ecole->setRne('0310211L');
$ecole->setCirconscription($em->merge($this->getReference('circo1')));
$ecole->setLibelle('FRANCE');
$ecole->setAdresse('6 AVENUE DE Test');
$ecole->setTelphone('05xxxxxxxx');
$ecole->setEmail('[email protected]');
$em->persist($ecole);
$em->flush();
$this->addReference('ecole', $ecole);
}
However, i get a NotNullConstraintViolationException
from Doctrine related to an integrity violation .
An exception occurred while executing 'INSERT INTO etablissement (rne, libe
lle, adresse, telphone, email, circo_id) VALUES (?, ?, ?, ?, ?, ?)' with pa
rams ["0310241U", null, null, null, null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'libelle' cann
ot be null
Though the value have been defined in the fixture. What could probably be the source of this error?or Why the insert generated by doctrine doesn't contain the values?
In your school fixture try to change the argument of setCirconscription()
:
public function load(ObjectManager $em) {
$ecole = new Etablissement();
$ecole->setRne('0310211L');
$ecole->setCirconscription($this->getReference('circo1'));
/.../
}
And do not forget to load Circuit Fixture
before School Fixture