0
votes

I've got custom entity repository (let's say CategoryRepository) that returns Doctrine entities. I also have newly created entity (let's say Product) that I want to persist. Product is related to Category and, in that case, Product is the owning side of the relationship so I've got following code:

$category = $categoryRepository->customGetCategory($someCriteria);
$product = new Product();
$product->setCategory($category);
$em->persist($product);

and result is

[Doctrine\ORM\ORMInvalidArgumentException]
A new entity was found through the relationship 'Acme\SomethingBundle\Entity\Product#category' that was not configured to cascade persist operations for entity: blahblah. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"})

For now I'm aware that all entities returned by custom repository methods using \Doctrine\ORM\Query::getResult() method where Query object is returned by EntityManager::createQuery($dql) factory method are detached by default. So I've got entity returned by repository that exists in database and I can't find a way for doctrine to have it managed just like any entity returned by f. ex. $repository->findBy() method.

Could anyone point me in right direction with this? I'd really like to solve that, it's killing me.

1
could you please post your Category and Product entities? (only the code snippets of the relation between them) - ponciste

1 Answers

0
votes

This is probably one of the top 5 Doctrine questions asked. Just difficult to search for. Could try searching on the error message.

The problem is that Category::setProduct is never being called. Update your Product entity with:

class Product
{
    public function setCategory($category);
    {
        $this->category = $category;
        $category->setProduct($this); // *** Add this
    }
}