Symfony2 - 通过关系找到了新实体 - 但它一直存在

时间:2015-04-23 09:49:17

标签: symfony doctrine-orm

我正在为Symfony项目制作一些数据固定装置。我在循环中创建实体。这两个实体是相互关联的(SchoolClass在学校)。但即使我在连接SchoolClass之前坚持学校,我仍然有这个错误:

[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();

    }
}

`

我正在观看数据库,但即使没有错误,也不会在完整脚本结束之前插入行。 这是$ manager-&gt; flush()的正常行为?我该怎么办呢?

0 个答案:

没有答案