Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Issue; Unable to persist entity if adding items to a relation

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

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

      <entity name="CG\Content" table="cg_content">
          <id name="id" type="integer" column="Id">
              <generator strategy="AUTO" />
          </id>
            <one-to-many field="tags" target-entity="CG\Tag" mapped-by="tagContent">
                 <join-column name="ContentId" referenced-column-name="Id" nullable="false"  fetch="FETCH" />
                <cascade>
                    <cascade-persist/>
                </cascade>
            </one-to-many>
</doctrine-mapping> 

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
      <entity name="CG\Tag" table="cg_contenttags">
            <!-- docterin requires bi-directional relationships for many to one -->
            <many-to-one field="tagContent" target-entity="CG\Content" inversed-by="tags">
                <join-column name="ContentId" referenced-column-name="Id" />
            </many-to-one>
      </entity>
</doctrine-mapping>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

      <entity name="CG\Term" table="cg_tags">
          <id name="id" type="integer" column="Id">
              <generator strategy="AUTO" />
          </id>

          <field name="name" column="Name" type="string" />
      </entity>
</doctrine-mapping> 

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

      <entity name="CG\TermAssociation" table="cg_contenttags"  inheritance-type="SINGLE_TABLE">
             <!-- surrogate PK for simplicity -->
             <id name="id" type="integer" column="Id">
                  <generator strategy="AUTO" />
              </id>

            <discriminator-column name="TaxonomyId" type="integer" />
            <discriminator-map>
                <discriminator-mapping value="1" class="CG\Tag" />
                <discriminator-mapping value="2" class="CG\Category" />
            </discriminator-map>


            <many-to-one field="content" target-entity="CG\Content" >
                <join-column  fetch="FETCH" name="ContentId" referenced-column-name="Id"/>
            </many-to-one >


            <many-to-one field="term" target-entity="CG\Term" >
                <join-column  fetch="FETCH" name="TagId" referenced-column-name="Id" />
                <cascade>
                    <cascade-persist/>
                </cascade>
            </many-to-one >
      </entity>
</doctrine-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

  1. receive json that describes changes to the content object
  2. 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
  3. copy over the fields from the json object to my content object
  4. call $content->tags->clear()
  5. loop through each of the tags in the json object. These are just strings. I need to recreate $content->tags
    1. See if there is a tag for this contentId and termName in the database. If so, add it to the tags collection. Done.
    2. 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.
    3. 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.

like image 885
i8abug Avatar asked Dec 27 '22 17:12

i8abug


1 Answers

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.

like image 151
i8abug Avatar answered Jan 04 '23 17:01

i8abug