【问题标题】:Data Fixtures many to one self referencing数据夹具多对一自引用
【发布时间】:2017-09-13 16:40:04
【问题描述】:

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

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;
    }

我怎样才能做到这一点?我做错了什么?

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    那是因为你的对象在你使用的时候没有被创建

    如the doc 中所述,使用addReference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多