0
votes

I'm using this function to copy content of a manyToMany relationship table

private function cloneProductDistributor($origin_distributor, $destiny_distributor){
        $products = $origin_distributor->getProducts();
        foreach ($products as $product){
            $destiny_distributor->addProduct($product);
        }
        $this->em->persist($destiny_distributor);
        $this->em->flush();
    }

in the entity we had

/**
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", mappedBy="distributors")
     */
    private $products;

/**
     * @return mixed
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * Add product
     *
     * @param Product $product
     *
     * @return Distributor
     */
    public function addProduct(Product $product)
    {
        $this->products[] = $product;

        return $this;
    }

Debugging I have checked that the content of $destiny_distributor is correct but when I check the database is not stored,do I missed something?

This is typically caused by failure to properly cross link the entities. You are adding $product to $destiny_distributor but are you adding $destiny_distributor to $product? There are many similar questions on this topic. - Cerad