数据夹具多对一自我参考

时间:2017-09-13 08:37:15

标签: symfony doctrine

我使用DoctrineFixturesBundle来填充我的数据库50000条记录。我希望有很多员工对一个老板。但是现在当我填充我的数据库时,我在boss_id中只有null值 我的加载功能:

public function load(ObjectManager $manager)
    {

        for ($i = 0; $i <= 50000; $i++) {
            $employee = new Employee();
            if($i == 0) {
                $employee->setPosition('founder');
            }
            $employee->setFullName('new employee'.$i);
            $employee->setSalary(50000 - $i);
            $employee->setStartDate(new \DateTime());
            if($i > 0) {
                $employee->setBoss($manager->find(Employee::class,1));
                $employee->setPosition('boss');
            } elseif ($i >= 1000) {
                $id = rand(2,1000);
                $employee->setBoss($manager->find(Employee::class,$id));
                $employee->setPosition('top-manager');
            } elseif ($i >= 5000) {
                $id = rand (1000,4999);
                $employee->setBoss($manager->find(Employee::class,$id));
                $employee->setPosition('manager');
            } elseif ($i >= 10000){
                $id = rand(5000,9999);
                $employee->setBoss($manager->find(Employee::class,$id));
                $employee->setPosition('worker');
            }
            $manager->persist($employee);

        }
        $manager->flush();
    }

我的实体员工:

 /**
     * @var Employee
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
     */
    private $boss;
public function setBoss(\AppBundle\Entity\Employee $boss = null)
    {
        $this->boss = $boss;

        return $this;
    }

我该怎么做?我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是因为使用它时不会创建对象

按照the doc

中的说明使用addReference