【问题标题】:Why is my PersistentCollection empty?为什么我的 PersistentCollection 是空的?
【发布时间】: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


【解决方案1】:

我不确定你是如何创建文档的,但这是我最好的选择:

Waypoint::$comments 未映射为反面,因此 ODM 期望在数据库中的 waypoint.comments 字段中提供引用列表。很可能它不存在(即您没有明确地将新的 Comment 添加到 Waypoint 中的集合中),这就是为什么您在查询航路点时看到空集合,但在查询 cmets 时有结果的原因。鉴于您在Comment 映射中有inversedBy="comments",我认为您忘记将Waypoint::$comments 设置为反面:

/**
 * @var ArrayCollection
 * @MongoDB\ReferenceMany(targetDocument="Comment", mappedBy="waypoint")
 */
private $comments;

【讨论】:

  • 是的,我错过了mappedBy="waypoint" 部分,导致看起来缺少参考。谢谢,我只是需要添加部分,现在它工作正常。
猜你喜欢
  • 2017-05-31
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多