我正在尝试创建一个简单的查询,以便在一个查询中获取所有相关实体(参见Doctrine - Get entity and relationships in one query)。由于某些原因,学说遇到了错误:
调用PersistentCollection中非对象上的成员函数add()
完全堆栈跟踪:
Error: Call to a member function add() on a non-object in \vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php line 177
at n/a
in \vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php line 177
at Doctrine\ORM\PersistentCollection->hydrateAdd()
in \vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 456
at Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateRowData()
in \vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 179
at Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateAllData()
in \vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\AbstractHydrator.php line 140
at Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll()
in \vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php line 804
at Doctrine\ORM\AbstractQuery->execute()
in \vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php line 574
at Doctrine\ORM\AbstractQuery->getResult()组映射:
Group:
type: entity
table: groups
id:
id:
type: integer
generator:
strategy: AUTO
oneToMany:
influences:
targetEntity: Influence
mappedBy: group
cascade: [ "persist" ]影响制图:
Influence:
type: entity
table: influences
id:
organisation:
associationKey: true
group:
associationKey: true
fields:
influence:
type: integer
manyToOne:
organisation:
targetEntity: Organisation
inversedBy: influences
group:
targetEntity: Group
inversedBy: influences下面是我的查询生成器逻辑:
$builder->select('g, i')
->from('Group', 'g')
->join('g.game', 'game')
->leftJoin('g.influences', 'i', Join::WITH, 'i.organisation = :org')
->setParameter('org', $organisation)
->orderBy('g.name')
->getQuery();谢谢你的暗示。
发布于 2014-08-29 18:15:34
这个问题的原因总的来说是很容易的。
我创建了不同的实体类,并在某些点将默认值(如空数组)添加到类字段中。
这将导致上述错误。
我这样做是为了测试我的业务逻辑本地,没有任何数据库连接或类似于Behat。
删除空的array()解决了我的问题。
为了与Behat兼容,我需要一个setter来重新设置一个空数组,或者扩展这个类,并添加一个构造函数来默认字段。
也许其他人对此有另一个想法。
https://stackoverflow.com/questions/25572240
复制相似问题