since 2 weeks, we are having this problem while trying to flush new elements:
CRITICAL: Doctrine\\ORM\\ORMInvalidArgumentException:
A new
Refreshing the entity in question helped my case.
/* $item->getProduct() is already set */
/* Add these 3 lines anyway */
$id = $item->getProduct()->getId();
$reference = $this->getDoctrine()->getReference(Product::class, $id);
$item->setProduct($reference);
/* Original code as follows */
$quote->getItems()->add($item);
$this->getDoctrine()->persist($quote);
$this->getDoctrine()->flush();
Despite my $item
already having a Product
set elsewhere (turns out it was set via a different instance of EntityManager
), I was still getting the error.
So this is a hack of sorts, by retrieving id
of the existing product, and then retrieving a reference of it, and using setProduct
to "refresh" the whatever connection. I later fixed it by ensuring to have only a single instance of EntityManager
in my code.
In my case a too early call of
$this->entityManager->clear();
caused the problem. It also disappeared by only doing a clear on the recent object, like
$this->entityManager->clear($capture);
I got this error too when tried to add new entity.
A new entity was found through the relationship 'Application\Entity\User#chats'
that was not configured to cascade persist operations for entity: ###.
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"}).
My case was that I tried to save entity, that shouldn't be saved. Entity relations was filled and tried to be saved (User
has Chat
in Many2Many, but Chat was a temporary entity), but there were some collisions.
So If I use cascade={"persist"}
I get unwanted behaviour - trash entity is saved. My solution was to remove non-saving entity out of any saving entities:
// User entity code
public function removeFromChats(Chat $c = null){
if ($c and $this->chats->contains($c)) {
$this->chats->removeElement($c);
}
}
Saving code
/* some code witch $chat entity */
$chat->addUser($user);
// saving
$user->removeFromChats($chat);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
I had the same problem and it was the same EntityManager
. I wanted to insert an object related ManyToOne
. And I don't want a cascade
persist
.
Example :
$category = $em->find("Category", 10);
$product = new Product();
$product->setCategory($category)
$em->persist($product);
$em->flush();
This throws the same exception for me.
So the solution is :
$category = $em->find("Category", 10);
$product = new Product();
$product->setCategory($category)
$em->merge($product);
$em->flush();
My answer is relevant for topic, but not very relevant for your particular case, so for those googling I post this, as the answers above did not help me.
In my case, I had the same error with batch-processing entities that had a relation and that relation was set to the very same entity.
WHAT I DID WRONG:
When I did $this->entityManager->clear();
while processing batch of entities I would get this error, because next batch of entities would point to the detached related entity.
WHAT WENT WRONG:
I did not know that $this->entityManager->clear();
works the same as $this->entityManager->detach($entity);
only detaches ALL of the repositorie`s entities.
I thought that $this->entityManager->clear();
also detaches related entities.
WHAT I SHOULD HAVE DONE:
I should have iterated over entities and detach them one by one - that would not detach the related entity that the future entities pointed to.
I hope this helps someone.
The error: 'Comment#capture' that was not configured to cascade persist operations for entity
The problem:
/**
* @ORM\ManyToOne(targetEntity="Capture", inversedBy="comments")
* @ORM\JoinColumn(name="capture_id", referencedColumnName="id",nullable=true)
*/
protected $capture;
dont configured the cascade persist
try with this:
/**
* @ORM\ManyToOne(targetEntity="Capture", inversedBy="comments", cascade={"persist", "remove" })
* @ORM\JoinColumn(name="capture_id", referencedColumnName="id",nullable=true)
*/
protected $capture;