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.