如何通过非主键引用“教义2”中的多对一关联

时间:2019-02-18 17:32:42

标签: php doctrine-orm doctrine many-to-one

据我了解,Doctrine需要多对一关联,以便在一侧引用主键。 (请参见herehere

如何解决在教义2中由唯一键引用的多对一关联?

在下面的内容中,我将展示到目前为止所提出的内容并遇到上述问题。要使其在我的上下文中起作用,必须进行哪些更改?

就我而言:

除其他属性外,项目还具有snapshotIdcidcid代表内部项目编号。在某些更改下,将拍摄快照并将其保留为唯一的snapshotId(包括关联表中的所有相关条目-此处未显示)。快照ID为空(空)的条目是当前状态。

正在ProjectHistory中跟踪项目的更改。出于演示目的,该实体具有属性functionmodifyDatemodifyUserId

该项目与ProjectHistory具有一对多关联。 ProjectHistory与Project的多对一关联。前者按预期工作,后者带来了开头提到的错误。

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(
 *     indexes={
 *         @ORM\Index(
 *             name="cid",
 *             columns={"cid"}
 *         ),
 *     },
 *     uniqueConstraints={
 *         @ORM\UniqueConstraint(
 *             name="unique_snapshot_per_project",
 *             columns={"snapshot_id", "cid"}
 *         ),
 *     },
 * )
 */
class Project
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     * @var string
     */
    private $id;

    /**
     * @ORM\Column(type="guid", unique=true, nullable=true)
     * @var string|null
     */
    private $snapshotId;

    /**
     * @ORM\Column()
     * @var string
     */
    private $cid;

    /**
     * @ORM\OneToMany(targetEntity="ProjectHistory", mappedBy="projectCid", cascade={"persist"})
     * @var Collection|ProjectHistory[]
     */
    private $projectHistories;

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

    public function toArray(): array
    {
        return [
            'id'               => $this->getId(),
            'snapshotId'       => $this->getSnapshotId(),
            'cid'              => $this->getCid(),
            'projectHistories' => $this->getProjectHistoriesAsArray(),
        ];
    }

    public function getProjectHistoriesAsArray(): array
    {
        $projectHistories = [];
        foreach ($this->getProjectHistories() as $projectHistory) {
            $projectHistories[] = $projectHistory->toArray();
        }

        return $projectHistories;
    }

    public function getId(): ?string
    {
        return $this->id;
    }

    public function setId(string $id): void
    {
        $this->id = $id;
    }

    public function getSnapshotId(): ?string
    {
        return $this->snapshotId;
    }

    public function setSnapshotId(string $snapshotId): void
    {
        $this->snapshotId = $snapshotId;
    }

    public function getCid(): string
    {
        return $this->cid;
    }

    public function setCid(string $cid): void
    {
        $this->cid = $cid;
    }

    public function getProjectHistories(): Collection
    {
        return $this->projectHistories;
    }

    /**
     * @param ArrayCollection|ProjectHistory[] $projectHistories
     * @return Project
     */
    public function addProjectHistories(ArrayCollection $projectHistories): Project
    {
        foreach ($projectHistories as $projectHistory) {
            if (! $this->projectHistories->contains($projectHistory)) {
                $this->projectHistories->add($projectHistory);
                $projectHistory->setProject($this);
            }
        }

        return $this;
    }

    /**
     * @param ArrayCollection|ProjectHistory[] $projectHistories
     * @return Project
     */
    public function removeProjectHistories(ArrayCollection $projectHistories): Project
    {
        foreach ($projectHistories as $projectHistory) {
            if ($this->projectHistories->contains($projectHistory)) {
                $this->projectHistories->remove($projectHistory);
            }
        }

        return $this;
    }
}
<?php

namespace App\Entity;

use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class ProjectHistory
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     * @var string
     */
    private $id;

    /**
     * @ORM\Column()
     * @var string
     */
    private $function;

    /**
     * @ORM\Column(type="datetime")
     * @var Datetime
     */
    private $modifyDate;

    /**
     * @ORM\Column(type="guid")
     * @var string
     */
    private $modifyUserId;

    /**
     * @ORM\ManyToOne(targetEntity="Project", inversedBy="projectHistories")
     * @ORM\JoinColumn(name="project_cid", referencedColumnName="cid")
     * @var Project
     */
    private $project;

    public function toArray(): array
    {
        return [
            'id'           => $this->getId(),
            'projectCid'   => $this->getProjectCid(),
            'function'     => $this->getFunction(),
            'modifyDate'   => $this->getModifyDateAsString(),
            'modifyUserId' => $this->getModifyUserId(),
        ];
    }

    public function getModifyDateAsString(): string
    {
        if (null === $this->getModifyDate()) {
            return '';
        }

        return $this->getModifyDate()->format(DateTimeInterface::ATOM);
    }

    public function getProjectCid(): ?string
    {
        if (null !== $this->getProject()) {
            return $this->getProject()->getCid();
        }

        return null;
    }

    public function getId(): ?string
    {
        return $this->id;
    }

    public function setId(string $id): void
    {
        $this->id = $id;
    }

    public function getFunction(): string
    {
        return $this->function;
    }

    public function setFunction($function): void
    {
        $this->function = $function;
    }

    public function getModifyDate(): DateTime
    {
        return $this->modifyDate;
    }

    public function setModifyDate(DateTime $modifyDate): void
    {
        $this->modifyDate = $modifyDate;
    }

    public function getModifyUserId(): string
    {
        return $this->modifyUserId;
    }

    public function setModifyUserId(string $modifyUserId): void
    {
        $this->modifyUserId = $modifyUserId;
    }

    public function getProject(): ?Project
    {
        return $this->project;
    }

    public function setProject(Project $project): void
    {
        $this->project = $project;
    }
}

任何帮助将不胜感激。让我知道,如果您需要更多信息。

1 个答案:

答案 0 :(得分:1)

通常,如果您需要进行此类操作,我的意思是使用非主键进行引用,则说明您的设计存在问题。阅读问题时,我想到的第一件事是Event Soursing。 ES for PHP很好地体现了-prooph。不幸的是,将现有项目重构为ES并非易事,但也许可以帮助您更好地理解历史更改和快照的概念。

根据当前情况,我可以说Project并不是快照。您可以修改模型并创建与ProjectSnapshot相关的Project实体,然后ProjectHistory将与ProjectSnapshot相关。另外,我不建议您在快照中存储任何关联(和外键),而只保存相关实体的标识符。请注意loggable extension,可能会有用。