1
votes

I have an object that is of type Doctrine\ODM\MongoDB\DocumentRepository and need to change how data is fetched, now it uses $this->findby but I need something more complicated and would like to use a doctrine query.

In all the examples I seem to be needing a EntityManager to invoke createQuery on but don't know how to get to it from within a Doctrine\ODM\MongoDB\DocumentRepository instance. There is no $this->getEntityManager function.

The class definition seem to be missing a method to get an entitymanager as well.

There is a way to create a querybuilder ($this->createQueryBuilder()) and that may have a way to get the entity manager but this must be another type of query builder:

$this->createQueryBuilder()->getEntityManager() undefined method: getEntityManager. Even though the name would suggest it's a querybuilder it really isn't.

Is there a way to execute dql from a Doctrine\ODM\MongoDB\DocumentRepository?

[update]

The reason for this is because I need to search on a date field that isn't a date. It consists of 3 string fields (year; a 4 length string, month; a 2 length string and day; a 2 lenght string). If the current day is Monday then it needs to search saturday and sunday as well. Current buggy code uses findby query and sometimes produces something like this:

$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')

I need something like:

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')
  )

The findby doesn't seem to provide something that can let me do such a query.

1

1 Answers

3
votes

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

It seems like you should be able to use the query builder object that's provided by $this->createQueryBuilder('MyEntity'). Try something like this

$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();