Php Symfony 2条令ODM参考
因此,我有以下两份文件: 壁纸Php Symfony 2条令ODM参考,php,mongodb,symfony,doctrine-orm,doctrine-odm,Php,Mongodb,Symfony,Doctrine Orm,Doctrine Odm,因此,我有以下两份文件: 壁纸 /** * @MongoDB\Document( * collection="wallpapers", * repositoryClass="Application\Bundle\DefaultBundle\Repository\WallpaperRepository" * ) */ class Wallpaper { /** * @MongoDB\Id(strategy="auto") * @var string
/**
* @MongoDB\Document(
* collection="wallpapers",
* repositoryClass="Application\Bundle\DefaultBundle\Repository\WallpaperRepository"
* )
*/
class Wallpaper
{
/**
* @MongoDB\Id(strategy="auto")
* @var string $id
*/
private $id;
/**
* @var ArrayCollection
*
* @MongoDB\ReferenceMany(
* targetDocument="Category"
* )
*
*
*/
private $categories;
/**
* Constructor
*/
public function __construct()
{
$this->categories = new ArrayCollection();
}
}
类别:
/**
* @MongoDB\Document(collection="categories")
*/
class Category
{
/**
* @MongoDB\Id(strategy="auto")
* @var string $id
*/
private $id;
/**
* @MongoDB\Field(type="boolean")
*
* @var bool $published
*/
private $published;
}
我需要选择所有只包含已发布类别的壁纸。。。
我尝试了很多解决方案,我通过谷歌搜索找到了。但仍然没有找到解决我问题的办法
我的问题是:
$wallpapers = $this->createQueryBuilder('ApplicationDefaultBundle:Wallpaper')
->field('categories.published')->equals(true)
->getQuery()
->execute()
但它不起作用:(
有什么建议吗?在关系数据库上,您可以通过连接来实现,但是在mongodb中,您可以:
取消壁纸文档中引用类别的规格化:
/**
* @MongoDB\EmbedMany(targetDocument="Category")
*/
private $categories;
(注意下面的例子)
这样,您的查询将起作用(但类别将在每张墙纸中重复)
或者做这个简单的把戏(如果类别数量不太多):
如果发布的类别只有100-200个,这个技巧就可以了。我会通过只选择“id字段”而不水合结果来进一步优化类别查询。只需在第一个查询生成器中添加
->fields(array(“id”)->hydrate(false)
,而不是$c->getId()
使用$c[''id']
。
//Get all the ids of the published categories:
$categories = $this->createQueryBuilder('ApplicationDefaultBundle:Category')
->field('published')->equals(true)
->getQuery()
->execute()
$categoriesIds = array();
foreach($categories as $c) {
$categoriesIds[] = $c->getId();
}
//Get the wallpapers for that categories:
$wallpapers = $this->createQueryBuilder('ApplicationDefaultBundle:Wallpaper')
->field('categories.$id')->in($categories)
->getQuery()
->execute()