与OneTomany协会坚持实体后,外键保持空虚



给定的是以下两个实体类

<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity()
 * @ORMTable()
 */
class Tree
{
    /**
     * @ORMId()
     * @ORMColumn(type="guid")
     * @ORMGeneratedValue(strategy="UUID")
     * @var string
     */
    private $id;
    /**
     * @ORMOneToMany(targetEntity="Apple", mappedBy="tree", cascade={"persist"})
     * @var Collection
     */
    private $apples;
    public function __construct()
    {
        $this->setApples(new ArrayCollection());
    }
    public function toArray(): array
    {
        return [
            'id' => $this->getId(),
        ];
    }
    public function getId(): string
    {
        return $this->id;
    }
    public function setId(string $id): void
    {
        $this->id = $id;
    }
    public function getApples(): Collection
    {
        return $this->apples;
    }
    public function setApples(Collection $apples): void
    {
        $this->apples = $apples;
    }
}
<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity()
 * @ORMTable()
 */
class Apple
{
    /**
     * @ORMId()
     * @ORMColumn(type="guid")
     * @ORMGeneratedValue(strategy="UUID")
     * @var string
     */
    private $id;
    /**
     * @ORMManyToOne(targetEntity="Tree", inversedBy="apples")
     * @var Tree
     */
    private $tree;
    public function toArray(): array
    {
        return [
            'id' => $this->getId(),
        ];
    }
    public function getId(): string
    {
        return $this->id;
    }
    public function setId(string $id): void
    {
        $this->id = $id;
    }
    public function getTree(): Tree
    {
        return $this->tree;
    }
    public function setTree(Tree $tree): void
    {
        $this->tree = $tree;
    }
}

数据库架构看起来不错,除了apple.tree_id无效。在这种情况下,这已经是一个问题吗?

我坚持以下内容:

<?php
declare(strict_types = 1);
namespace AppService;
use AppEntityApple;
use AppEntityTree;
use DoctrineORMEntityManager;
class Gardener
{
    private $entityManager;
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function plantTree(): array
    {
        $entityManager = $this->entityManager;
        $tree          = new Tree();
        $blueApple = new Apple();
        $redApple  = new Apple();
        $tree->getApples()->add($blueApple);
        $tree->getApples()->add($redApple);
        $entityManager->persist($tree);
        $entityManager->flush();
        return (array) $tree;
    }
}

执行持久和冲洗时,没有任何错误或警告。一棵树是两个苹果条目是商店, apple.tree_id始终为null。

看来我对实体课程有错误的配置,但不确定是什么。我还尝试添加JoinColumn注释@ORMJoinColumn(name="tree_id", referencedColumnName="id"),但没有任何区别。

我需要做些更改,才能正确设置appe.tree_id

您缺少加法器&amp;*ToMany侧的拆卸功能。

如果您使用Symfony> 4,则用:

替换setApples功能
public function addApple(Apple $apple): Tree
{
    $this->apples->add($apple);
    return $this;
}
public function removeApple(Apple $apple): Tree
{
    $this->apples->removeElement($apple);
    return $this;
}

如果您使用的是Zend Framework 3,请用:

替换setApples功能
public function addApples(array $apples): Tree
{
    foreach ($apples as $apple) {
        if (! $this->apples->contains($apple) {
            $this->apples->add($apple)
            $apple->setTree($this);
        }
    }
    return $this;
}
public function removeApples(array $apples): Tree
{
    foreach ($apples as $apple) {
        if($this->apples->contains($apple) {
            $this->apples->remove($apple);
        }
    }
    return $this;
}

阅读了与协会文档合作的阅读,该文档显示了示例并说明如何更新'n'hort。

最新更新