教义问题;如果将项目添加到关系中,则无法保留实体

发布于 2024-11-19 10:23:45 字数 18121 浏览 1 评论 0原文

我在设置 Doctrine 时遇到一些困难,希望一些大师可以帮助我。如果我尝试单独保存 MainEntity,它工作得很好。但是,如果我将“标签”添加到该实体的标签集合中,则会收到错误。

类似的实体

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;
}

这是我的信息:我最终有与此映射

<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>

,这是我遇到的错误

( ! ) 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

编辑1:

这是我的 SaveContent 函数中的行

public function saveContent($content)
    {
        $this->entityManager->persist($content);
        $this->entityManager->flush();
    }

保存内容非常容易,但是,要创建 $content,我执行以下

  1. 接收描述内容对象更改的 json
  2. 检查我收到的 json 是否有 id。如果是这样,我从数据库中获取内容对象。如果没有,我将创建一个新的内容对象,
  3. 将字段从 json 对象复制到我的内容对象,
  4. 调用 $content->tags->clear()
  5. 循环遍历 json 对象中的每个标签。这些只是字符串。我需要重新创建 $content->tags
    1. 查看数据库中是否存在此 contentId 和 termName 的标签。如果是这样,请将其添加到标签集合中。完成。
    2. 如果没有,请查看数据库中是否有与 termName 匹配的术语。如果是这样,请创建一个新标签,为其分配术语和内容,然后将其添加到标签中。完成。
    3. 如果没有,则创建一个新标签和一个新术语,为其分配术语和内容,然后将其添加到标签中。完成。

进行 var 转储显示我的 $content 对象符合预期。

编辑 2:已解决

我的内容对象毕竟不符合预期。我已将数组添加到标签集合中,而不是 CG\Tag。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀念你的温柔 2024-11-26 10:23:46

我也遇到了同样的问题 - 也涉及标签(许多标签与数据库中的许多文章相关)。

我有以下指令:

                    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);

正确工作的代码是:(

                    foreach ($tags as $vv) {
                        $tobj = $this->getDoctrine()
                            ->getRepository("MainBundle:Tag")
                                ->findOneByTag($vv); // returns one object
                        //...
                        $artic->getTags()->add($tobj);

注意 findONEby,而不是 findby)。

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).

耶耶耶 2024-11-26 10:23:45

只是回答这个问题来关闭它。如果其他人也有同样的问题,也许这会有所帮助。

我的内容对象毕竟不符合预期。我已将一个数组添加到标签集合中,而不是 CG\Tag。这导致集合不是 Doctrine 所期望的,因此发出警告

警告:spl_object_hash() 期望参数 1 为对象、数组
给定

警告比实际错误提供的信息要多得多。

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.

阳光下的泡沫是彩色的 2024-11-26 10:23:45

小提示:请注意 addTag 方法和 setTag 方法之间的区别。

<?php

class Article
{
    /**
     * @var Tags
     *
     * @ORM\ManyToMany(targetEntity="Tags", inversedBy="article", cascade={"all"})
     * @ORM\JoinTable(name="article_tags",
     *   joinColumns={
     *     @ORM\JoinColumn(name="article_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
     *   }
     * )
     */
    private $tag;

    public function __construct()
    {
        $this->tag = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addTag(\Acme\CoreBundle\Entity\Tag $tag)
    {
        $this->tag[] = $tag;
    }

    // the set method is useful to automatic form purposes
    public function setTag(\Doctrine\Common\Collections\ArrayCollection $tag)
    {
        $this->tag = $tag;
    }
}

?>

A little tip: pay attention to the difference between the addTag method and the setTag methods.

<?php

class Article
{
    /**
     * @var Tags
     *
     * @ORM\ManyToMany(targetEntity="Tags", inversedBy="article", cascade={"all"})
     * @ORM\JoinTable(name="article_tags",
     *   joinColumns={
     *     @ORM\JoinColumn(name="article_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
     *   }
     * )
     */
    private $tag;

    public function __construct()
    {
        $this->tag = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addTag(\Acme\CoreBundle\Entity\Tag $tag)
    {
        $this->tag[] = $tag;
    }

    // the set method is useful to automatic form purposes
    public function setTag(\Doctrine\Common\Collections\ArrayCollection $tag)
    {
        $this->tag = $tag;
    }
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文