2

我正在使用学说/mongodb-odm-bundle,但我遇到了一个问题:我无法从文档中获取引用的行(或者我只是不知道如何执行此操作..)我有 2 个一对一的文档许多参考是这样的:首先

/**
 * @MongoDB\Document(collection="categories")
 */
class Category
{
    /**
     * @var integer $id
     *
     * @MongoDB\Id(strategy="auto")
     */
    private $id;

    /**
     * @var string $name
     *
     * @MongoDB\String
     * @Assert\NotBlank()
     * @Assert\MinLength(3)
     */
    private $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Application\Bundle\DefaultBundle\Document\Wallpaper", mappedBy="category")
     */
    private $files;
.................
    /**
     * Set files
     *
     * @param array $files
     */
    public function setFiles($files)
    {
        $this->files = $files;
    }

    /**
     * Get files
     *
     * @return array $files
     */
    public function getFiles()
    {
        return $this->files;
    }

..................
第二

/**
 * @MongoDB\Document(collection="wallpapers")
 */
class Wallpaper
{
    /**
     * @var string $id
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;
    /**
     * @MongoDB\ReferenceOne(targetDocument="Application\Bundle\DefaultBundle\Document\Category", inversedBy="files")
     */
    private $category;

    /**
     * Get category
     *
     * @return Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set category
     *
     * @param Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

}

这是来自控制器的代码:

$category = $dm->getRepository('ApplicationDefaultBundle:Category')->findOneBy(...);
$wallpapers = $category->getFiles();

$wallpapers 和 $document->files 为 NULL。我如何检索与类别相关的记录?以及如何从混凝土墙纸对象中获取类别?有没有像标准ORM那样的“JOIN”模拟?

4

3 回答 3

2

映射看起来正确。我认为您的问题可能与查询有关。我还将检查壁纸集合是否具有正确的文档,其中类别字段填充了正确的 DBRef 对象数据。

$category = $dm->getRepository('Application\Bundle\DefaultBundle\Document\Wallpaper')->findOneById($id);
$wallpapers = $category->getFiles(); // Will return a cursor to the wallpaper objects
foreach ($wallpapers as $wallpaper) {
     do stuff
}

If this isn't the issue, can you paste the full query you are trying and a sample of data from the two collections.

于 2012-03-21T19:56:30.200 回答
2

Are you sure that DoctrineORM removed out your project? I had this problem. I removed out my project DoctrineORM and it is worked.

于 2012-07-07T10:07:00.887 回答
0

There is no "join" like in SQL, the ODM will make separate queries and combine them into the object. By default doctrine does this lazily when accessing that portion.

As Jamie said the query and data are key parts to helping here.

于 2012-03-21T20:07:35.050 回答