Search Yii使用条件搜索并添加参数
我有一个表格,我想在我的数据库中搜索。我用所有参数创建对象,但有一个问题。当写入一个文本字段时,搜索工作正常,查询运行正常。当写入两个或多个文本字段时,参数不起作用,并且查询执行失败: 在哪里 (((id_reservation=:id_reservation)和(start=:start))和(end=:end))和 (fkCustomer.first_name=:first_name)) params不能代替Search Yii使用条件搜索并添加参数,search,activerecord,yii,criteria,Search,Activerecord,Yii,Criteria,我有一个表格,我想在我的数据库中搜索。我用所有参数创建对象,但有一个问题。当写入一个文本字段时,搜索工作正常,查询运行正常。当写入两个或多个文本字段时,参数不起作用,并且查询执行失败: 在哪里 (((id_reservation=:id_reservation)和(start=:start))和(end=:end))和 (fkCustomer.first_name=:first_name)) params不能代替 $criteria=new CDbCriteria; $crite
$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;
这是因为在每个
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”,它将在没有冒号的情况下工作。这是因为在每个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”,它将在没有冒号的情况下工作。您还有以下替代方法
$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;
您还可以选择以下选项
$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;
您好,我想这与我以前遇到的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);
您好,我想这与我以前遇到的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);
жббббббббббб
$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;
жббббббббббб
$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的汇编程序是什么我不明白。它与编程有关吗?它的汇编程序是什么我不明白。它与编程有关吗?