【问题标题】:dynamic query in doctrine with AND/ORAND/OR 学说中的动态查询
【发布时间】:2013-10-29 20:32:51
【问题描述】:

我需要根据用户选择过滤查询中的结果。我向查询发送了一个包含所有选择的选项的数组。

这是数组的样子:

array (size=6)
'registered' => int 2
'active' => int 1
'notactive' => int 0
'preregistered' => int 1
'male' => string 'm' (length=1)
'female' => string 'f' (length=1)

我需要执行此操作(假设用户选择了此选项):

(registered OR preregistered) AND (active OR notactive) AND (male)

这是迄今为止我拥有的代码,花了很多时间和精力,但它没有给我正确的结果:

public function customReport($data){
        // var_dump($data);die();

    $this->qb = $this->em->createQueryBuilder();
    $andX = $this->qb->expr()->andX();

    $this->qb->select('u')
    ->from('models\User','u');          

    foreach($data as $key=>&$value){

        if($key == "registered"){
            if(in_array("preregistered", $data)){
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.status', '?5'),
                    $this->qb->expr()->eq('u.status', '?6')
                    );
            }else{
                $condition = $this->qb->expr()->eq('u.status', '?5');
            }
            $andX->add($condition);
        }else{
            if($key == "preregistered"){
                $condition = $this->qb->expr()->eq('u.status','?6');
            }
            $andX->add($condition);
        }

        if($key == "active"){
            if(in_array("notactive", $data)){
                //va un OR entre estas dos condiciones
                // $condition = ('u.active = 1 OR u.active = 0');
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.active', '?1'),
                    $this->qb->expr()->eq('u.active', '?2')
                    );
                    // var_dump($condition);die();
            }else{
                $condition = $this->qb->expr()->eq('u.active', '?1');
            }
            $andX->add($condition);
        }else{
            if($key == "notactive"){
                $condition = $this->qb->expr()->eq('u.active', '?2');
            }
            $andX->add($condition);
        }

        if($key == "male"){
            if(in_array("female", $data)){
                $condition = $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.gender', '?3'),
                    $this->qb->expr()->eq('u.gender', '?4')
                    );
            }else{
                $condition = $this->qb->expr()->eq('u.gender', '?3');
            }
            $andX->add($condition);
        }else{
            if($key == "female"){
                $condition = $this->qb->expr()->eq('u.gender', '?4');
            }
            $andX->add($condition);
        }
        // var_dump($condition);die();
    }

    $this->qb->add('where', $andX);

    if(in_array('active', $data)){
        $this->qb->setParameter(1,$data['active']);
    }
    if(in_array('notactive', $data)){
        $this->qb->setParameter(2,$data['notactive']);
    }
    if(in_array('male', $data)){
        $this->qb->setParameter(3, $data['male']);
    }
    if(in_array("female", $data)){
        $this->qb->setParameter(4, $data['female']);
    }
    if(in_array('registered', $data)){
        $this->qb->setParameter(5,$data['registered']);
    }
    if(in_array('preregistered', $data)){
        $this->qb->setParameter(6,$data['preregistered']);
    }


    $query = $this->qb->getQuery();

    var_dump($query);die();
    $obj = $query->getResult();


    if (!empty($obj)){
        return $obj;

        return false;
    }       

}

它返回的是一个格式错误的结果,这里是 ir 的一部分:

string 'SELECT u FROM models\User u WHERE (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.status = ?5 OR u.status = ?6) AND (u.active = ?1 OR u.active = ?2) AND (u.active = ?1 OR u.active = ?2) AND (u.active = ?1 OR u.active = ?2) AND u.active = ?2 AND u.active = ?2 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND u.status = ?6 AND (u.gender = ?3 OR u.gender = ?4)' (length=451)

而且,它说“女性”是一个未定义的索引(如果用户选择此选项,则不会出现)

【问题讨论】:

  • 我建议更改数组的填充方式,例如,如果选择重新“活动”存在,则生成“并且在 () 中活动”。这样做,构建您的动态查询将是孩子们的游戏。
  • 你说我应该向查询发送一个二维数组吗?

标签: php sql codeigniter doctrine dql


【解决方案1】:

你能在那些查询构建器中使用 orWhere 或 andWhere 吗? ref

// NOTE: ->where() overrides all previously set conditions
    //
    // this will remove all other orWhere or andWhere before it. so use it only initially
    public function where($where);

    // Example - $qb->andWhere($qb->expr()->orX($qb->expr()->lte('u.age', 40), 'u.numChild = 0'))
    public function andWhere($where);

    // Example - $qb->orWhere($qb->expr()->between('u.id', 1, 10));
    public function orWhere($where);

你也可以尝试在expr上设置参数

    $this->qb->expr()->orX(
                    $this->qb->expr()->eq('u.gender', '?3'),
                    $this->qb->expr()->eq('u.gender', '?4'))
                    ->setParameters(array(3 => 'value for ?3', 4 => 'value for ?4'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多