学说从反面替换一对一关系

时间:2021-04-30 12:08:53

标签: php doctrine-orm doctrine

我有两个实体 ProductPrint 一对一关系,其中 Product 是父实体,Print 是子实体,但“拥有方”是一个 Print(在数据库 print 表中将有一个 product_id 和外键)

产品:

<?php

declare(strict_types=1);

namespace App\Entity;

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

/**
 * @ORM\Entity()
 */
class Product
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    public int $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    public string $name;


    /**
     * @ORM\Column(type="text")
     */
    public string $description;

    /**
     * @ORM\Column(type="boolean")
     */
    public bool $available;


    /**
     * @ORM\OneToOne(targetEntity="Print", mappedBy="product", cascade={"persist", "remove"},  orphanRemoval=true)
     */
    public Print $print;


    public function __construct(
        int $id,
        string $name,
        string $description,
        bool $available,
    ) {
        $this->id = $id;
        $this->name = $name;
        $this->description = $description;
        $this-available = $available;
    }

    public function udpate(
        string $name,
        string $description,
        bool $available,
        Print $print,
    ) {
        $this->name = $name;
        $this->description = $description;
        $this-available = $available;
        $this->replacePrint($$print);
    }


    public function replacePrint(Print $print): void
    {
        $this->print = $print;
    }

}

并打印:

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Print
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    public int $id;

    /**
     * @ORM\Column(type="string")
     */
    public array $key;

    /**
     * @ORM\Column(type="string")
     */
    public array $value;

    /**
     * @ORM\OneToOne(targetEntity="Product", inversedBy="print")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
     */
    public Product $product;

    public function __construct(Product $product, string $key, string $value)
    {
        $this->product = $blueprint;
        $this->key = $key;
        $this->value = value;
    }
}

当我插入新产品时一切正常

$product = new Product($id, $name, $description, $available);
$product->replacePrint(new Print($product, $key, $value));

$em->persist($product);
$em->flush

它正在插入产品和相关印刷品,为印刷品生成新的 id

但是现在当我想更新产品时:

$product = $em->find(Product::class, $id);
$product->udpate($name, $description, $available);
$product->replacePrint(new Print($product, $key, $value));

$em->persist($product);
$em->flush

我收到错误 Cannot assign null to property App\\Entity\\Print::$id of type int

1 个答案:

答案 0 :(得分:0)

您必须保留 New Print 实体:

$product = $em->find(Product::class, $id);
$product->udpate($name, $description, $available);
$print = new Print($product, $key, $value);
$product->replacePrint($print);

$em->persist($print);

$em->persist($product);
$em->flush();