Doctrine Issue; Unable to persist entity if adding items to a relation-Collection of common programming errors
I’m having some difficulty setting up Doctrine and am hoping some guru’s can give me a hand. If I try to save a MainEntity on its own, it works fine. However, if I add a “tag” to the tags collection of this entity, I get an error.
Here is my info: I have entities that are similar to this
namespace CG;
class Content
{
public $id;
public $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
}
/* this is single table inheritance */
class Tag extends TagAssociation
{
public $id;
public $term;
public $content;
}
class Term
{
public $id;
public $name;
}
Mapping
finally, here are the errors I am getting
( ! ) Warning: spl_object_hash() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 1073
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4672 6934672 Doctrine\ORM\UnitOfWork->getEntityState( ) ..\UnitOfWork.php:572
11 0.4672 6934704 spl_object_hash ( ) ..\UnitOfWork.php:1073
( ! ) Warning: spl_object_hash() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 573
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4677 6934704 spl_object_hash ( ) ..\UnitOfWork.php:573
( ! ) Warning: spl_object_hash() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 601
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4680 6934704 Doctrine\ORM\UnitOfWork->persistNew( ) ..\UnitOfWork.php:583
11 0.4680 6934736 spl_object_hash ( ) ..\UnitOfWork.php:601
( ! ) Warning: spl_object_hash() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 882
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4680 6934704 Doctrine\ORM\UnitOfWork->persistNew( ) ..\UnitOfWork.php:583
11 0.4685 6934816 Doctrine\ORM\UnitOfWork->scheduleForInsert( ) ..\UnitOfWork.php:621
12 0.4685 6934848 spl_object_hash ( ) ..\UnitOfWork.php:882
( ! ) Warning: spl_object_hash() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 405
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4689 6935112 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:584
11 0.4689 6935144 spl_object_hash ( ) ..\UnitOfWork.php:405
( ! ) Warning: ReflectionProperty::getValue() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 408
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4689 6935112 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:584
11 0.4693 6935344 ReflectionProperty->getValue( ) ..\UnitOfWork.php:408
( ! ) Warning: ReflectionProperty::getValue() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 408
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4623 6772872 Doctrine\ORM\UnitOfWork->computeChangeSets( ) ..\UnitOfWork.php:256
8 0.4669 6932368 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:510
9 0.4671 6934472 Doctrine\ORM\UnitOfWork->computeAssociationChanges( ) ..\UnitOfWork.php:495
10 0.4689 6935112 Doctrine\ORM\UnitOfWork->computeChangeSet( ) ..\UnitOfWork.php:584
11 0.4696 6935424 ReflectionProperty->getValue( ) ..\UnitOfWork.php:408
( ! ) Warning: get_class() expects parameter 1 to be object, array given in C:\wamp\www\Content\Doctrine\ORM\UnitOfWork.php on line 839
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4703 6935072 Doctrine\ORM\UnitOfWork->getCommitOrder( ) ..\UnitOfWork.php:279
8 0.4712 6960832 get_class ( ) ..\UnitOfWork.php:839
( ! ) Warning: class_parents() [function.class-parents]: object or string expected in C:\wamp\www\Content\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 223
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4703 6935072 Doctrine\ORM\UnitOfWork->getCommitOrder( ) ..\UnitOfWork.php:279
8 0.4715 6960784 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\UnitOfWork.php:841
9 0.4715 6960784 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:257
10 0.4716 6960816 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:170
11 0.4716 6960936 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:246
12 0.4716 6961088 class_parents ( ) ..\ClassMetadataFactory.php:223
( ! ) Warning: array_reverse() expects parameter 1 to be array, boolean given in C:\wamp\www\Content\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 223
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4703 6935072 Doctrine\ORM\UnitOfWork->getCommitOrder( ) ..\UnitOfWork.php:279
8 0.4715 6960784 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\UnitOfWork.php:841
9 0.4715 6960784 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:257
10 0.4716 6960816 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:170
11 0.4716 6960936 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:246
12 0.4720 6961448 array_reverse ( ) ..\ClassMetadataFactory.php:223
( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\Content\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 223
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4703 6935072 Doctrine\ORM\UnitOfWork->getCommitOrder( ) ..\UnitOfWork.php:279
8 0.4715 6960784 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\UnitOfWork.php:841
9 0.4715 6960784 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:257
10 0.4716 6960816 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:170
11 0.4716 6960936 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:246
( ! ) Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in C:\wamp\www\Content\Doctrine\ORM\Mapping\ClassMetadata.php on line 66
( ! ) ReflectionException: Class does not exist in C:\wamp\www\Content\Doctrine\ORM\Mapping\ClassMetadata.php on line 66
Call Stack
# Time Memory Function Location
1 0.0130 437368 {main}( ) ..\index.php:0
2 0.3809 3813352 CG\handleRequest( ) ..\index.php:181
3 0.3817 3813384 CG\saveContent( ) ..\index.php:92
4 0.4617 6772280 CG\Database->saveContent( ) ..\index.php:33
5 0.4623 6772872 Doctrine\ORM\EntityManager->flush( ) ..\Database.php:120
6 0.4623 6772872 Doctrine\ORM\UnitOfWork->commit( ) ..\EntityManager.php:334
7 0.4703 6935072 Doctrine\ORM\UnitOfWork->getCommitOrder( ) ..\UnitOfWork.php:279
8 0.4715 6960784 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\UnitOfWork.php:841
9 0.4715 6960784 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:257
10 0.4716 6960816 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:170
11 0.4727 6971528 Doctrine\ORM\Mapping\ClassMetadataFactory->newClassMetadataInstance( ) ..\ClassMetadataFactory.php:263
12 0.4728 6973768 Doctrine\ORM\Mapping\ClassMetadata->__construct( ) ..\ClassMetadataFactory.php:362
13 0.4728 6974008 ReflectionClass->__construct( ) ..\ClassMetadata.php:66
Edit 1:
Here are the lines from my SaveContent function
public function saveContent($content)
{
$this->entityManager->persist($content);
$this->entityManager->flush();
}
Save content is pretty easy, however, to create the $content, I am doing the following
- receive json that describes changes to the content object
- check if the json I receive has an id. If so, I get the content object from the database. If not, I create a new content object
- copy over the fields from the json object to my content object
- call $content->tags->clear()
- loop through each of the tags in the json object. These are just strings. I need to recreate $content->tags
-
- See if there is a tag for this contentId and termName in the database. If so, add it to the tags collection. Done.
- If not, see if there is a term in the database that matches the termName. If so, create a new tag, assign the term and content to it, and add it to tags. Done.
- if not, create a new tag and a new term, assign the term and content to it, and add it to tags. Done.
Doing a var dump shows that my $content object is as expected.
Edit 2: Solved
My content object wasn’t as expected after all. I had added an array to the tags collection rather than a CG\Tag.
-
Just answering this question to close it. If anyone else has the same problem, maybe this can help.
My content object wasn’t as expected after all. I had added an array to the tags collection rather than a CG\Tag. This resulted in the collection not being what Doctrine had expected hence the warning
Warning: spl_object_hash() expects parameter 1 to be object, array given
The warning was much more informative than the actual error.
-
Just for practical purposes for Symfony2 developers: Take care to the difference between the
addCategory
method and thesetCategory
method! -
I had exaclty the same problem – also with tags (many tags related to many articles in DB).
I had the following instruction:
foreach ($tags as $vv) { $tobj = $this->getDoctrine() ->getRepository("MainBundle:Tag") ->findByTag($vv); // returns ARRAY of objects (= not wanted and not expected by Doctrine) //... $artic->getTags()->add($tobj);
And properly working code is:
foreach ($tags as $vv) { $tobj = $this->getDoctrine() ->getRepository("MainBundle:Tag") ->findOneByTag($vv); // returns one object //... $artic->getTags()->add($tobj);
(notice findONEby, not findby).
Originally posted 2013-11-27 12:27:26.