我正在尝试创建一个简单的查询以在一个查询中获取所有相关实体(请参阅Doctrine - 在一个查询中获取实体和关系 )。 由于某些原因,学说遇到错误:
在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();谢谢你的任何提示。
I am trying to create a simple query to get all related entities in one query (see Doctrine - Get entity and relationships in one query). For some reasons doctrine encounters an error:
Call to a member function add() on a non-object in PersistentCollection
Full stacktrace:
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 mapping:
Group: type: entity table: groups id: id: type: integer generator: strategy: AUTO oneToMany: influences: targetEntity: Influence mappedBy: group cascade: [ "persist" ]Influence mapping:
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: influencesAnd here is my query builder logic:
$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();Thanks for any hints.
最满意答案
这个问题的原因通常很容易。
我创建了不同的实体类,并在某些点添加了像空字符这样的默认值到类字段。 这导致上述错误。
我已经完成了测试我的业务逻辑本地,没有任何数据库连接或与Behat类似。
删除空array()解决了我的问题。
为了与Behat兼容我需要一个字段的setter来再次设置一个空数组或扩展这个类并添加一个构造函数来默认该字段。 也许其他人对此有另一种想法。
The cause of this issue is in general very easy.
I have created different entity classes and added at some points default values like empty array to the class field. This results into the error above.
I have done it to test my business logic local without any database connection or similar with Behat.
Removing the empty array() solved my problem.
To be compatible with Behat I need a setter for the field to set again an empty array or extend this class and add a constructor to default the field. Perhaps someone else have another idea about this.
更多推荐
发布评论