简体   繁体   English

doctrine 从反面替换一对一关系

[英]doctrine replace onetoone relation from inverse side

I have two entities Product and Print with one to one relation, where Product is a parent entity and Print is a child, but "owning side" is a Print (in database print table 'll have a product_id and foreign key)我有两个实体ProductPrint具有一对一的关系,其中Product是父实体, Print是子实体,但“拥有方”是Print (在数据库print表中将有product_id和外键)

Product:产品:

<?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;
    }

}

and 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;
    }
}

when I'm inserting new product all is fine当我插入新产品时一切都很好

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

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

it's inserting product and related print, generating new id for a print它正在插入产品和相关的印刷品,为印刷品生成新的 id

But now when I want to update prodcut:但是现在当我想更新产品时:

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

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

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

You have to persist the New Print entity:您必须保留 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM