如何在 NelmioAlice 的实体构造函数中为 ArrayCollection 设置固定装置

How to setup fixtures for ArrayCollection at entity constructor in NelmioAlice

我已经阅读并尝试了文档 this 部分中描述的所有内容,但我无法正常工作。这是实体的构造函数:

public function __construct()
{
    $this->targets = new ArrayCollection();
    $this->targetBrand = new ArrayCollection();
}

我尝试使用 __construct: false 绕过,但出现此错误:

[Symfony\Component\Yaml\Exception\ParseException] Unable to parse at line 11 (near " name: ").

灯具是这样的:

\PDI\PDOneBundle\Entity\Brand:
    # 3 Brand per Company
    brand{1..3}:
       __construct: false
       name: <name()>
       generic_name: <name()>
       logo_url: <imageUrl(128,128)>
       description: <paragraph()>
       isi_required: <boolean(35)>
       isi_text: <realText()>
       isi_pdf_url: <url()>
       pi_required: <boolean(35)>
       pi_text: <realText()>
       pi_pdf_url: <url()>
       inactive: <boolean(35)>
       company: @company*
       createdAt: <dateTimeThisYear()>
       updatedAt: <dateTimeThisYear()>

如果我没有设置 __construct 那么错误会变成另一个:

[Symfony\Component\Debug\Exception\ContextErrorException] Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 555 and defined

怎么了?我应该如何设置夹具?

编辑:

我找到了 this 但是我如何在我尝试设置的装置上使用这种方法,可以给我一些建议吗?

编辑 1:

我有点困惑,因为 Brand 映射到 TargetBrand 实体,而 TargetBrandBrand 实体反转,请参阅下面的实体:

class Brand
{
    // other class properties

    /**
     * @ORM\OneToMany(targetEntity="TargetBrand" , mappedBy="brand")
     */
    protected $targetBrand;

    protected $targets;

    public function __construct()
    {
        $this->targets = new ArrayCollection();
        $this->targetBrand = new ArrayCollection();
    }

    // other class methods

    public function getTargets()
    {
        $targets = new ArrayCollection();
        foreach ($this->targetBrand as $target) {
            $targets[] = $target->getTarget();
        }

        return $targets;
    }

    public function setTargets($targets)
    {
        foreach ($targets as $target) {
            $targetBrand = new TargetBrand();
            $targetBrand->setBrand($this);
            $targetBrand->setTarget($target);
            $this->addTargetBrand($targetBrand);
        }
    }

    public function addTargetBrand($targetBrand)
    {
        $this->targetBrand[] = $targetBrand;
    }

    public function removeTargetBrand($targetBrand)
    {
        return $this->targetBrand->removeElement($targetBrand);
    }
}


class TargetBrand
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     */
    protected $priority;

    /**
     * @var Target
     *
     * @ORM\ManyToOne(targetEntity="Target", inversedBy="targetBrand")
     * @ORM\JoinColumn(name="targets_id", referencedColumnName="id")
     * */
    protected $target;

    /**
     * @var Brand
     *
     * @ORM\ManyToOne(targetEntity="Brand", inversedBy="targetBrand")
     * @ORM\JoinColumn(name="brands_id", referencedColumnName="id")
     * */
    protected $brand;

    public function setPriority($priority)
    {
        $this->priority = $priority;

        return $this;
    }

    public function getPriority()
    {
        return $this->priority;
    }

    public function setTarget(Target $targets = null)
    {
        $this->target = $targets;

        return $this;
    }

    public function getTarget()
    {
        return $this->target;
    }

    public function setBrand(Brand $brand)
    {
        $this->brand = $brand;

        return $this;
    }

    public function getBrand()
    {
        return $this->brand;
    }
}

如果我运行使用的灯具如下:

这是我之前提到的一套:

$set->addFile(__DIR__.'/Territory.yml', 'yaml');
$set->addFile(__DIR__.'/Representative.yml', 'yaml');
$set->addFile(__DIR__.'/TargetBrand.yml', 'yaml');
$set->addFile(__DIR__.'/Target.yml', 'yaml');
$set->addFile(__DIR__.'/Brand.yml', 'yaml');

$set->addFile(__DIR__.'/Company.yml', 'yaml');
$set->addFile(__DIR__.'/Media.yml', 'yaml');
$set->addFile(__DIR__.'/Message.yml', 'yaml');

$set->addFile(__DIR__.'/Email.yml', 'yaml');

并设置BrandTargetBrand灯具如下:

我得到了这个错误:

[Symfony\Component\Debug\Exception\ContextErrorException] Catchable Fatal Error: Argument 1 passed to PDI\PDOneBundle\Entity\TargetBrand::setTarget() must be an instance of PDI\PDOneBundle\Entity\Target, instance of PDI\PDOneBundle\Entity\TargetBrand given, called in /var/www/html/reptooln_admin/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php on line 506 and defined

\PDI\PDOneBundle\Entity\Brand:
    # 3 Brand per Company
    brand{1..3}:
       name: <name()>
       generic_name: <name()>
       logo_url: <imageUrl(128,128)>
       description: <paragraph()>
       isi_required: <boolean(35)>
       isi_text: <realText()>
       isi_pdf_url: <url()>
       pi_required: <boolean(35)>
       pi_text: <realText()>
       pi_pdf_url: <url()>
       inactive: <boolean(35)>
       company: @company*
       createdAt: <dateTimeThisYear()>
       updatedAt: <dateTimeThisYear()>
       targetBrand: @targetBrand*

\PDI\PDOneBundle\Entity\TargetBrand:
  # 10000 TargetBrand
  targetBrand{1..1000}:
      priority: <randomDigitNotNull()>
      target: @target*
      brand: @brand*

在这种情况下加载固定装置的正确顺序是什么?

注意: $targets 不再需要,所以不要处理那个

如果您定义了 setter,即 setTarget()setTargetBrand()addTarget()addTargetBrand(),以下应该有效:

\PDI\PDOneBundle\Entity\Brand:
    brand{1..3}:
       (define your brands)
\PDI\PDOneBundle\Entity\Target:
    target{1..3}:
       (define your targets)
\PDI\PDOneBundle\Entity\TargetBrand:
    targetBrand{1..3}:
       target: @target<current()>
       brand: @brand<current()>

\PDI\PDOneBundle\Entity\Brand:
    brand{1..3}:
        (..other definitions...)
        targets: [@target1, @target2, @target3]
        targetBrand: [@targetBrand1, @targetBrand2, @targetBrand3]

对我来说,以这种方式添加相关的(多对多)对象固定装置

ordersServices: ['@service_*']