0

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

WHERE ((((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;
4

4 回答 4

3

这是因为在每个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',它可以在没有冒号的情况下工作。

于 2013-05-25T23:33:42.797 回答
1

您还有以下选择

$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;
于 2013-07-17T05:30:27.107 回答
0

嗨,我认为这与我之前使用 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);
于 2014-03-26T21:46:41.790 回答
0

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

 $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;
于 2015-04-15T07:23:45.773 回答