我正在使用doctrine/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;
}
}
这是来自 Controller 的代码:
$category = $dm->getRepository('ApplicationDefaultBundle:Category')->findOneBy(...);
$wallpapers = $category->getFiles();
$wallpapers 和 $document->files 为 NULL。我如何检索与类别相关的记录?我如何从混凝土壁纸对象中获取类别?是否有像标准 ORM 中那样的“JOIN”模拟?
最佳答案
映射看起来正确。我认为您的问题可能与查询有关。我还会检查壁纸集合是否具有正确的文档,其中的类别字段填充了正确的 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
}
如果这不是问题,您能否粘贴您正在尝试的完整查询以及两个集合中的数据示例。
关于mongodb - 教义2 MongoDB获取对象的子记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791301/