Skip to content

Cascade persist not working for replasinc child entity #8664

Closed

Description

@bogdan-dubyk

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)

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

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 , why cascade persist not automatically persist Print???

Activity

  1. locked and limited conversation to collaborators on Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions