I'm making some datafixtures for a Symfony project. I create entities in loops. The two entities are linked (SchoolClass is in School). But even if I persist School before linking SchoolClass to it, I still have this error:
[Doctrine\ORM\ORMInvalidArgumentException]
A new entity was found through the relationship 'Schoolit\SchoolBundle\SchoolClass#school' that was not configured to cascade persist operations for entity: 337 - Paul & Sons. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or
configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
`
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
set_time_limit(10*60); // Setting max time to 10 minutes
ini_set("memory_limit","1G"); // Setting memory limit to 1 Go
if($this->environment == 'dev')
{
$faker = Factory::create('fr_FR');
// -------------------------
// Load developpement data
// ------------------------
// SCHOOLS
// --------------------
$schools = array();
for($i=0; $i<30; $i++)
{
$entity = new School();
$entity->setName($faker->company);
$manager->persist($entity);
$schools[] = $entity;
}
$manager->flush();
// CLASSES
// --------------------
for($i=0; $i<100; $i++)
{
$entity = new SchoolClass();
$entity->setSchool($schools[array_rand($schools)]);
$manager->persist($entity);
// To clear memory
$manager->flush();
$manager->clear();
}
$manager->flush();
}
}
`
I'm watching the database, but even if there is no error, no row is inserted before the end of the full script. It is the normal behavior of $manager->flush() ? What can I do about it ?