与OneToMany关联保留实体后,外键保持为空

时间:2019-02-14 15:39:52

标签: doctrine-orm annotations associations

给出以下两个实体类

<?php

namespace App\Entity;

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

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

    /**
     * @ORM\OneToMany(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 App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\ManyToOne(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 App\Service;

use App\Entity\Apple;
use App\Entity\Tree;
use Doctrine\ORM\EntityManager;

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始终为空。

似乎我在实体类上配置有误,但不确定是什么。我还尝试添加JoinColumn注释@ORM\JoinColumn(name="tree_id", referencedColumnName="id"),但没有任何区别。

要正确设置appe.tree_id,我需要进行哪些更改?

1 个答案:

答案 0 :(得分:1)

您缺少*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;
}

已阅读Working with Association文档,其中显示了示例并解释了如何向后更新'n'。