Doctrine2 - 如何在MongoDB中查询引用的文档

时间:2013-11-07 12:45:25

标签: php mongodb symfony doctrine-orm doctrine

我有两个集合,categoriesjobsjobs集合在categories内引用。

现在我在CategoryRepository中有一个名为getWithActiveJobs()的方法,此方法返回所有类别,其中包含未过期已激活的参考作业。

问题是当我运行此方法时,没有返回任何内容。我是MongoDB的新用户,请告诉我如何在jobs内查询CategoryRepository

这是我的收藏品(不包括制定者和吸气者以及其他方法):

Category.php

namespace Ibw\JobeetBundle\Document;

use Ibw\JobeetBundle\Utils\Jobeet;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="categories", repositoryClass="Ibw\JobeetBundle\Repository\CategoryRepository")
 */
class Category
{

    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Job")
     */
    protected $jobs = array();

    /**
     * @MongoDB\ReferenceMany(targetDocument="Affiliate")
     */
    protected $affiliates = array();

    /**
     * @MongoDB\String
     */
    protected $slug;
}

Job.php

namespace Ibw\JobeetBundle\Document;

use Ibw\JobeetBundle\Utils\Jobeet;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="jobs", repositoryClass="Ibw\JobeetBundle\Repository\JobRepository")
 * @Assert\GroupSequence({"Form", "Job"})
 */
class Job
{
    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    protected $id;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
         * @Assert\Choice(callback="getTypeValues", groups={"Form"})
     */
    protected $type;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $company;

    /**
         * @MongoDB\String
     */
    protected $logo;

        /**
         * @Assert\Image(groups={"Form"})
         */
        protected $file;

    /**
         * @MongoDB\String
         * @Assert\Url(groups={"Form"})
     */
    protected $url;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $position;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $location;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $description;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $how_to_apply;

    /**
         * @MongoDB\String
         * @Assert\NotBlank()
     */
    protected $token;

    /**
         * @MongoDB\Boolean
     */
    protected $is_public;

    /**
         * @MongoDB\Boolean
     */
    protected $is_activated;

    /**
         * @MongoDB\String
         * @Assert\NotBlank(groups={"Form"})
         * @Assert\Email(groups={"Form"})
     */
    protected $email;

    /**
         * @MongoDB\Date
     */
    protected $expires_at;

    /**
         * @MongoDB\Date
     */
    protected $created_at;

    /**
         * @MongoDB\Date
     */
    protected $updated_at;

    /**
         * @MongoDB\ReferenceOne(targetDocument="Category", cascade={"persist"})
         * @Assert\NotBlank(groups={"Form"})
     */
    protected $category;
}

这是CategoryRepository.php方法:

public function getWithActiveJobs($limit = null)
{
    $qb = $this->createQueryBuilder()
                ->field('jobs')->prime(true)
                ->field('jobs.expires_at')->gt(date('Y-m-d H:i:s', time()))
                ->field('jobs.is_activated')->equals(true)
                ->sort('jobs.expires_at', 'DESC');

    if($limit)
    {
        $qb->limit($limit);
    }

    return $qb->getQuery()->execute();
}

这是MongoDB categories集合:

> db.categories.find().pretty()
{
    "_id" : ObjectId("527b884610fedf400d8b4589"),
    "name" : "Design",
    "slug" : "design"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458c"),
    "name" : "Administrator",
    "slug" : "administrator"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458a"),
    "jobs" : [
        DBRef("jobs", ObjectId("527b884610fedf400d8b4587"))
    ],
    "name" : "Programming",
    "slug" : "programming"
}
{
    "_id" : ObjectId("527b884610fedf400d8b458b"),
    "jobs" : [
        DBRef("jobs", ObjectId("527b884610fedf400d8b4588"))
    ],
    "name" : "Manager",
    "slug" : "manager"
}

1 个答案:

答案 0 :(得分:0)

从mongodb开始时这是一个常见错误:

你做不到JOIN

prime属性将获取Jobs文档,但仅在查询执行后才会获取,并且无效。

您可以尝试不同的方法:

使用不同的查询并获取活动作业的所有类别IDS:

db.Job.distinct( 'category.$id', { is_activated: true } )