exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'App\Lib\Domain\Datalayer\UnicodeLookups#lookupStatus' that was not configured to cascade persist operations for entity:
Searching through the various online sources was a bit of a nightmare. The best documentation I found was at http://www.krueckeberg.org/ where there were a number of clearly explained examples of various associations.
More useful information about association ownership was in the Doctrine manual, but I found a more succinct explanation in the answer to this question on StackOverflow.
Now I understood better about associations and ownership and was able to identify exactly what sort I was using and the syntax that was required. I was implementing a uni-directional many to one relationship, which is supposedly one of the most simple to map.
I had used the Doctrine reverse engineering tool to generate the stubs of my model. I was expecting it to be able to handle this particular relationship - the tool warns that it does not properly map all relationships but this particular one actually works out of the box.
{project root}/vendor/doctrine/orm/bin/doctrine orm:convert-mapping --from-database yml --namespace App\\Lib\\Domain\\Datalayer\\ .
{project root}/vendor/doctrine/orm/bin/doctrine orm:generate-entities --generate-methods=true --generate-annotations=true --regenerate-entities=true ../../../
{project root}/vendor/doctrine/orm/bin/doctrine orm:generate-proxies ../Proxies
Just as an aside to explain the paths and namespaces : I'm using Laravel 5 and put my domain model into app/Lib/Domain. I'm implementing a form of the repository design pattern so I have the following directory structure:
app/Lib/Domain
+--Entities
+--Proxies
+--Mappings
+--Repositories
+--Services
The mappings are not used at runtime but are used to generate the entities.
So my generated class looked like this:
namespace App\Lib\Domain\Datalayer;
/**
* Lookups
*
* @ORM\Table(name="lookups", indexes={@ORM\Index(name="IDX_4CEC819D037A087", columns={"lookup_status_id"}), @ORM\Index(name="IDX_4CEC8194D39DE23", columns={"camera_event_id"})})
* @ORM\Entity
*/
class Lookups
{
/**
* @var \App\Lib\Domain\Datalayer\LookupStatuses
*
* @ORM\ManyToOne(targetEntity="App\Lib\Domain\Datalayer\LookupStatuses", inversedBy="lookups", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="lookup_status_id", referencedColumnName="id")
* })
*/
private $lookupStatus;
}
My use case was:
- Find a lookup status from the table
- Call setLookupStatus on my lookup class
- Persist and flush the lookup class
- Error
After carefully reviewing all the documentation I linked above, and a great deal more, I realized that step 1 was the issue. Because I was getting the object out of cache Doctrine thought it was new.
Of course I couldn't persist the object as the help message suggested (it already existed so I got a key violation error). The mappings were actually correct.
So my advice for resolving this error message is to read through the documentation I linked above carefully and then make sure that Doctrine is actually aware of the entity you're using. You may need to persist it (if its new) or otherwise make sure its not cached.
Comments
Post a Comment