Fixing detatched Doctrine entity problems

I’m not sure WHY it happens, but sometimes Doctrine entities can become detatched from the entity manager (If anyone does, please comment below!).

If you’ve ever added an existing object to a new one and tried to persist, you may see this error:

[Doctrine\ORM\ORMInvalidArgumentException]
A new entity was found through the relationship ‘OAuth\Client#user’ that was not configured to cascade persist operations for entity: OAuth\OAuthUser@000
000007e2d73fd0000000054f34ea6. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist t
his association in the mapping for example @ManyToOne(..,cascade={“persist”}). If you cannot find out which entity causes the problem implement ‘OAuth\OA
uthUser#__toString()’ to get a clue.

In the case of my code above, the user entity had become detached somehow. Previously, I had managed to solve this problem by calling merge($entity) instead of persist($entity), but this is itself causes problems. If you have join column collections, these will no longer persist!

Therefore the answer is to re-attach the entity first!

public function create(Client $client)
{
$em = $this->getEntityManager();
$user = $client->getUser();
if ($em->getUnitOfWork()->getEntityState($user) !== UnitOfWork::STATE_MANAGED) {
$user = $em->merge($user);
$client->setUser($user);
}
$em->persist($client);
$em->flush();
return $client;
}
Advertisements

Fixing Git Submodule Issues

If you’ve set up a Git ‘Push to Deploy’ setup for your site based on my earlier post, you may or may not have run into some anomalous behaviour! Namely, when you run git submodule init, you get a message similar to this:

No submodule mapping found in .gitmodules for path 'js/jquery-browser-fingerprint'

Or even :

fatal: reference is not a tree: 48d1407779fb1e8dc6d3c6c6fb87df4c1cbc65ed
Unable to checkout '48d1407779fb1e8dc6d3c6c6fb87df4c1cbc65ed' in submodule path 'cms/js/flipswitch'

Thankfully, these issues are easily fixed. modules should be listed in a file in your root .gitmodules, and look like this:

[submodule "jquery-browser-fingerprint"]
 path = /js/jquery-browser-fingerprint
 url = git://github.com/carlo/jquery-browser-fingerprint.git

Listing your submodules in here will fix your first problem. The second issue is caused by referencing a commit that hasn’t been pushed! Did you copy project files from somewhere else? Try pushing the commit from your other project.

A clearer way to sort these issues however, is to clear whatever cache git has for those submodules. Typing this clears it.

git rm --cached js/jquery-browser-fingerprint

This should allow you to run git submodule init, and git submodule update. One last point. You don’t remember ever having to create this .gitmodules file before, so why now? Basically it allows you to set custom folders etc for the submodule. If you’re happy with the defaults, you should theoretically be able to skip the submodules file, and ┬ácan combine both git commands into this:

git submodule update --init

This updates everything and initialises in one! So to sum up, try removing the cached stuff in git, then run the combined update init file! Happy pushing, pulling, and deploying!