Doctrine 3 不创建外键 - PDOException: "Column 'tour_id' cannot be null"

Doctrine 3 does not create the foreign key - PDOException: "Column 'tour_id' cannot be null"

我正在尝试在数据库中插入一条新记录。

我有两个 tables bootstrap_tourbootstrap_tour_step.

id of boostrap_tour table 是bootstrap_tour_step table.

中的外键tour_id

对应实体如下:

BootstrapTour.php

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
 * @ORM\Id
 *
 * @JMS\Groups({"auth_read_postbootstraptours"})
 * @JMS\Type("integer")
 * @JMS\Accessor(getter="getId")
 */
protected $id;

/**
 * @var ArrayCollection[BootstrapTourStep]
 *
 * @ORM\OneToMany(targetEntity="BootstrapTourStep", mappedBy="bootstrapTour", cascade={"persist"})
 *
 * @JMS\Groups({"auth_read_postbootstraptours"})
 */
private $bootstrapTourSteps;

/**
 * Object instantiation.
 */
public function __construct()
{
    parent::__construct();

    $this->bootstrapTourSteps = new ArrayCollection();
}

/**
 * Sets a collection of BootstrapTourStep objects.
 *
 * @param ArrayCollection|null $bootstrapTourSteps
 *
 * @return BootstrapTour
 */
public function setBootstrapTourSteps(?ArrayCollection $bootstrapTourSteps): BootstrapTour
{
    $this->bootstrapTourSteps = $bootstrapTourSteps;
    return $this;
}

/**
 * Returns a collection of BootstrapTourStep objects.
 *
 * @return Collection[BootstrapTourStep]|null
 */
public function getBootstrapTourSteps(): ?Collection
{
    return $this->bootstrapTourSteps;
}

/**
 * Adds a Step to the tour.
 *
 * @return BootstrapTour
 */
public function addBootstrapTourStep(BootstrapTourStep $bootstrapTourStep): BootstrapTour
{
    $bootstrapTourStep->setBootstrapTour($this);
    $this->bootstrapTourSteps[] = $bootstrapTourStep;

    return $this;
}

BootstrapTourStep.php

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 *
 * @JMS\Groups({"auth_read_getbootstraptours"})
 * @JMS\Type("integer")
 * @JMS\Accessor(getter="getId")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="BootstrapTour", inversedBy="bootstrapTourSteps")
 * @ORM\JoinColumn(name="tour_id", referencedColumnName="id", nullable=false)
 * 
 * @JMS\Groups({"auth_read_postbootstraptours"})
 * @JMS\Type("EN\CentralAdmin\DoctrineBundle\Entity\BootstrapTour")
 * @JMS\Accessor(getter="getBootstrapTour", setter="setBootstrapTour")
 */
private $bootstrapTour;

/**
 * Gets the BootstrapTour 
 * 
 * @return BootstrapTour|null
 */
public function getBootstrapTour(): ?BootstrapTour
{
    return $this->bootstrapTour;
}

/**
 * Sets a BootstrapTour
 * 
 * @param BootstrapTour $bootstrapTour
 * @return BootstrapTourStep
 */
public function setBootstrapTour(BootstrapTour $bootstrapTour): BootstrapTourStep
{
    $this->bootstrapTour = $bootstrapTour;
    return $this;
}

/**
 * A list of reference proxies.
 *
 * @return array
 */
public function getReferenceProxies(): array
{
    return [ 
        'BootstrapTour'
    ];
}

我的控制器操作:

$bootstrapTourService = $this->getCentralAdminEntityService('BootstrapTour');
$bootstrapTourService->persist($tourType, true);

我能够select使用这个数据但是在添加新记录的情况下我得到以下异常:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tour_id' cannot be null

我该如何解决这个问题?

添加一组步骤时,您没有在步骤上设置与游览的关系。通过这种方式,步骤实体被添加到游览 步骤本身不知道它们属于哪个游览。

如果现在 doctrine 试图坚持这些步骤,他们对游览的引用将丢失,因此您会得到丢失的 tour_id 异常。

这个...

public function setBootstrapTourSteps(?ArrayCollection $bootstrapTourSteps): BootstrapTour
{
    $this->bootstrapTourSteps = $bootstrapTourSteps;

    return $this;
}

...应该是:

public function setBootstrapTourSteps(?ArrayCollection $bootstrapTourSteps): BootstrapTour
{
    $this->bootstrapTourSteps = new ArrayCollection();
    foreach ($bootstrapTourSteps as $step) {
      $step->setBootstrapTour($this);
      $this->bootstrapTourSteps->add($step);
    }

    return $this;
}