我在设立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,我正在执行以下操作
执行var转储显示我的$ content对象符合预期。
编辑2:已解决
我的内容对象毕竟不是预期的。我在标签集合中添加了一个数组而不是CG \ Tag。
答案 0 :(得分:8)
回答这个问题以关闭它。如果其他人有同样的问题,也许这可以帮助。
我的内容对象毕竟不是预期的。我在标签集合中添加了一个数组而不是CG \ Tag。这导致收集不是Doctrine所期望的,因此警告
警告:spl_object_hash()期望参数1为object,array 给定
警告比实际错误提供了更多信息。
答案 1 :(得分:2)
小提示:注意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;
}
}
?>
答案 2 :(得分:0)
我有同样的问题 - 也有标签(许多标签与数据库中的许多文章相关)。
我有以下指示:
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)。