首页 文章

在Doctrine \ ODM \ MongoDB \ DocumentRepository中创建原则查询

提问于
浏览
1

我有一个Doctrine \ ODM \ MongoDB \ DocumentRepository类型的对象,需要更改数据的获取方式,现在它使用$ this-> findby但我需要更复杂的东西,并且想要使用一个doctrine查询 .

在所有examples中,我似乎需要一个EntityManager来调用createQuery,但不知道如何从Doctrine \ ODM \ MongoDB \ DocumentRepository实例中获取它 . 没有$ this-> getEntityManager函数 .

class definition似乎缺少一个获取实体管理器的方法 .

有一种方法可以创建一个querybuilder($ this-> createQueryBuilder()),that可能有办法获取实体管理器,但这必须是另一种类型的查询构建器:

$this->createQueryBuilder()->getEntityManager() undefined方法:getEntityManager . 虽然这个名字会暗示它's a querybuilder it really isn' t .

有没有办法从Doctrine \ ODM \ MongoDB \ DocumentRepository执行dql?

[update]

这是因为我需要搜索不是日期的日期字段 . 它由3个字符串字段组成(年份; 4个长度字符串,月份; 2个长度字符串和日期; 2个长度字符串) . 如果当天是星期一那么它需要搜索星期六和星期日 . 当前的错误代码使用findby查询,有时产生如下内容:

$this->findBy(array(
        'data.type; => 6,
        'data.year' => '2014',
        'data.month' => '06',
        'data.day' => array( '$in' => ['02','01','0-1'])
//results in: where year=''2014' and month='06' and day in ('02','01','0-1')

我需要这样的东西:

Where
  type = 6
  and (
    (year='2014' and month='06' and day='02')
    OR (year='2014' and month='06' and day='01')
    OR (year='2014' and month='05' and day='31')
  )

findby似乎没有提供可以让我做这样的查询的东西 .

1 回答

  • 3

    http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html

    您似乎应该能够使用 $this->createQueryBuilder('MyEntity') 提供的查询构建器对象 . 尝试这样的事情

    $qb = $this->createQueryBuilder('data');
    
    $results = $qb
        ->field('type')->equals(6) //add quotes i.e. '6' if type is string
        ->addOr(
            $qb->expr()
            ->field('year')->equals('2014')
            ->field('month')->equals('06')
            ->field('day')->in(array('01','02'))
        )
        ->addOr(
            $qb->expr()
            ->field('year')->equals('2014')
            ->field('month')->equals('05')
            ->field('day')->equals('31')
        )->getQuery()->execute();
    

相关问题