【发布时间】:2017-10-17 14:58:49
【问题描述】:
我将 Symfony 3 框架与 Doctrine 和 MongoDB 一起使用。
我有两个处于 OneToMany 关系的文档。
/**
* Class Waypoint
* @package AppBundle\Document
* @MongoDB\Document(collection="waypoints", repositoryClass="AppBundle\Repository\WaypointRepository")
*/
class Waypoint
{
/**
* @var int
*
* @MongoDB\Id(strategy="auto")
*/
private $id;
/**
* @var ArrayCollection
* @MongoDB\ReferenceMany(targetDocument="Comment", cascade={"delete"})
*/
private $comments;
}
**
* Class Comment
* @package AppBundle\Document
* @MongoDB\Document(collection="comments", repositoryClass="AppBundle\Repository\CommentRepository")
*/
class Comment
{
/**
* @var int
*
* @MongoDB\Id(strategy="auto")
*/
private $id;
/**
* @var Waypoint
*
* @MongoDB\ReferenceOne(targetDocument="Waypoint", inversedBy="comments")
* @Assert\NotBlank()
*/
private $waypoint;
}
现在我从存储库查询中获取了我的 Waypoint 条目的一部分,并希望用 twig 显示它们。
/**
* WaypointRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class WaypointRepository extends DocumentRepository
{
public function getWaypointsForCruiseByPage(Cruise $cruise, $page)
{
$displayLimit = 10;
$amountToSkip = 0;
if ($page > 1)
{
$amountToSkip = ($page -1) * $displayLimit;
}
$qb = $this->createQueryBuilder()
->select()
->field('cruise')->equals($cruise)
->field('isAutoWaypoint')->equals(false)
->sort('date', -1)
->skip($amountToSkip)
->limit($displayLimit)
;
$qb
->addOr($qb->expr()->field('hasImage')->equals(true))
->addOr($qb->expr()->field('hasAudio')->equals(true))
->addOr($qb->expr()->field('description')->notEqual(''))
;
return $qb->getQuery()->toArray();
}
}
现在,我尝试将 {{ waypoint.comments.count }} 或 {{ waypoint.comments|length }} 始终为 0,即使我的 MongoDB 集合中有数据集。
如果我通过 Waypoint 的 ID 通过 CommentRepository 获取 cmets,我将获得预期的结果。
// returns the expected results
public function getAllCommentsForWaypoint(Waypoint $waypoint)
{
return $this->createQueryBuilder()
->select()
->field('waypoint')->equals($waypoint)
->getQuery()->toArray()
;
}
据我所知,映射很好,没有发现任何缺陷或错误。
为什么 PersistentCollection 是空的,事件虽然信息在集合中?
【问题讨论】:
-
“现在我正在从存储库查询中获取我的航点条目的一部分” - 请粘贴提到的查询
-
@malarzm 我已经编辑了问题
标签: php mongodb symfony doctrine-orm doctrine-odm