I have a one-to-many self-referecing relation and I want to clone the entity like follows:
product{
cloned_product : [23,32,32]
parent_product_id: 12
}
In doctrine I represented the relation as follows:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="parentProductId")
*/
private $clonedProduct;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="clonedProduct", cascade={"persist"})
* @JoinColumn(name="parent_product_id", referencedColumnName="id")
*/
private $parentProductId;
and the clone function:
public function __clone()
{
$this->setParentProductId($this->id);
if ($this->id){
$this->id =null;
}
}
and the method that calls it:
public function clone(Product $product){
$em = $this->container->get('doctrine.orm.entity_manager');
$clonedProduct = clone $product;
$em->persist($clonedProduct);
$em->flush();
return $clonedProduct;
}
but gives 500 error saying that it is returned entity of type /Product and integer is received. How to solve this?