【问题标题】:Yii search with criteria and add parametersYii 使用条件搜索并添加参数
【发布时间】:2013-05-25 22:29:31
【问题描述】:

我有一个表格,我想在我的数据库中搜索。我用所有参数创建对象,但我有一个问题。当在一个文本字段中写入时,搜索工作正常并且查询运行正确。当编写两个或多个文本字段参数不起作用并且我执行查询失败时:

在哪里 ((((id_reservation=:id_reservation) AND (start=:start)) AND (end=:end)) AND (fkCustomer.first_name=:first_name))

参数不会替换。

    $criteria=new CDbCriteria;
    $criteria->with =array('fkCustomer');
    if(!empty($start))
    {
        $criteria->addCondition('start=:start');
        $criteria->params=array(':start'=>$start);
    }
    if(!empty($end))
    {

        $criteria->addCondition('end=:end');
        $criteria->params=array(':end'=>$end);
    }
    if(!empty($merge->customer_name))
    {

        $criteria->addCondition('fkCustomer.first_name=:first_name');
        $criteria->params=array(':first_name'=>$merge->customer_name);
    }
    if(!empty($merge->customer_surname))
    {

        $criteria->addCondition('fkCustomer.last_name=:last_name');
        $criteria->params=array(':last_name'=>$merge->customer_surname);
    }
    if(!empty($merge->customer_email))
    {

        $criteria->addCondition('fkCustomer.email=:email');
        $criteria->params=array(':email'=>$merge->customer_email);
    }
    $criteria->limit = 100;

【问题讨论】:

    标签: search activerecord yii criteria


    【解决方案1】:

    这是因为在每个 if 块中,您都替换了 params 数组。在if 块中构建一个数组,然后将其添加到块外最后一行的$criteria->params

    例如:

    $criteria=new CDbCriteria;
    $criteria->with =array('fkCustomer');
    $my_params = array();
    
    if(!empty($end))
    {
        $criteria->addCondition('end=:end');
        $my_params['end'] = $end;
    }
    
    if(!empty($merge->customer_name))
    {
        $criteria->addCondition('fkCustomer.first_name=:first_name');
        $my_params['first_name'] = $merge->customer_name;
    }
    
    // other ifs ..
    
    //then
    $criteria->limit = 100;
    $criteria->params = $my_params;
    

    另外,如果我没记错的话,你不需要在 params 数组中写 ':end' 和 ':first_name',不用冒号也可以。

    【讨论】:

      【解决方案2】:

      您还有以下选择

      $criteria=new CDbCriteria;
      $criteria->with = 'fkCustomer';
      
      if(!empty($end))
      {
          $criteria->compare('end', $end);
      }
      
      if(!empty($merge->customer_name))
      {
          $criteria->compare('fkCustomer.first_name', $merge->customer_name);
      }
      
      // The following conditions ..
      
      // Limit:
      $criteria->limit = 100;
      

      【讨论】:

        【解决方案3】:

        您好,我认为这与我之前在使用 params 变量时遇到的问题相同,为了避免这个问题,我使用了 CMap::mergeArray 函数

        发生这种情况是因为每次条件通过变量时都会覆盖变量。

        这是避免它的语法,它是一个例子

        $criteria=new CDBCriteria;
        $criteria->addBetweenCondition("Date",$datestart,$dateend);
        $criteria->addCondition("Type=:type");
        //$criteria->params=array(":type"=>"1"); //This is wrong, overwrites addBetweenCondition params
        $criteria->params=CMap::mergeArray($criteria->params,array(
            ":type"=>"1",
        )); //This is ok, mantain all parameters in the params var
        $query=Model::findAll($criteria);
        

        【讨论】:

          【解决方案4】:

          можно обратиться напрямую

           $criteria=new CDbCriteria;
              $criteria->with =array('fkCustomer');
              if(!empty($start))
              {
                  $criteria->addCondition('start=:start');
                  $criteria->params['start']=$start;
              }
              if(!empty($end))
              {
          
                  $criteria->addCondition('end=:end');
                  $criteria->params['end']=$end;
              }
              if(!empty($merge->customer_name))
              {
          
                  $criteria->addCondition('fkCustomer.first_name=:first_name');
                  $criteria->params['first_name']=$merge->customer_name;
              }
              if(!empty($merge->customer_surname))
              {
          
                  $criteria->addCondition('fkCustomer.last_name=:last_name');
                  $criteria->params['last_name']=$merge->customer_surname;
              }
              if(!empty($merge->customer_email))
              {
          
                  $criteria->addCondition('fkCustomer.email=:email');
                  $criteria->params['email']=$merge->customer_email;
              }
              $criteria->limit = 100;
          

          【讨论】:

          • 什么是можно обратиться напрямую??
          • @mayank-jain 它是汇编程序 ;)
          • 我看不懂。和编程有关吗?
          猜你喜欢
          • 2015-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-05
          • 1970-01-01
          相关资源
          最近更新 更多