Doctrine: How to remove part of the where clause from a selected query inside a listener (preDqlSelect)?

My listener is part of the behavior that is to remove all is_published checks in the where clause of any select request being called. Adding a part to a sentence is very simple, but how to remove it.

There are some functions, such as Doctrine_Query->removeDqlQueryPart('where'), but which removes the full where clause, while I only need the part 'is_published = ?'that needs to be removed.

However, I could somehow handle this manually, with a regex or something else. But the tricky part is how to remove the parameter represented by the '?' from an array of relevant parameters (retrieved Doctrine_Query->getRawParams()).

So, I ask if there is a clean way to convert this kind of query:
...FROM Video v WHERE v.is_published = ? AND v.start_date < ? AND v.end_date > ?

To do this, I divided one and did not spoil the parameters represented by question marks:
...FROM Video v WHERE v.start_date < ? AND v.end_date > ?

This, of course, is a simple example, my queries are a little more complicated. Unfortunately, I stick to doctrine 1.0.x because of the symfony framework.

+5
source share
2 answers

The call $query->getDqlPart('where')returns the arrayparts of the where clause since they were added through functions where(), andWhere()etc. This way you can use this to find and delete the part you need.

. , ? , $params = $query->getParams();, where $params['where'], , $query->setParams($params);

+6

    $qb = <query builder>;
    $qb_where_part = $qb->getDqlPart('where')->getParts();
    $qb->resetDQLPart('where');
    // we know by dumping that it is an and operator in our case, generic way shoud take op in account
    //var_dump($qb->getDqlPart('where'));
    foreach ($qb_where_part as $where_clause) {
        if ('o.date > :date_debut' === $where_clause) continue;
        $qb->andWhere($where_clause);
    }
    $params = $qb->getParameters();
    $new_date_fin = null;
    foreach ($params as $key => $param) {
        if ($param->getName() === 'date_debut') {
            $new_date_fin = $param->getValue();
            $params->remove($key);
        }
        if ($param->getName() === 'date_fin' && $new_date_fin) {
            $param->setValue($new_date_fin);
            //var_dump($param->getValue());
        }
    }
    $qb->setParameters($params);
    var_dump($qb->getParameters());
    var_dump($qb->getDqlPart('where'));
+5

All Articles