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

别来无恙 提交于 2019-12-05 06:55:16
Joshua Coady

Calling $query->getDqlPart('where') will return an array of the parts of the where clause as they were added via the where(), andWhere(), etc functions. So you can use that to find and remove the part you want.

Then you have to deal with the params. While cycling through the where parts you would need to find all ? and count them and remember the numbers for any of the ones you remove and then call $params = $query->getParams(); and the where clause parameters will be in $params['where'] so you can remove them from there and then call $query->setParams($params);

Based Joshua Coady Answer

    $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'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!