To get the DB result in basis of condition
if($keyword!='')
build condition;
/*
array('conditions' => array("AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%",
"esl.esl_开发者_开发百科songname LIKE"=>"%".$songname."%")),
'limit' => $arrFind['limit'],
'page'=>$arrFind['page']));
*/
if(!name!='')
/*
array('conditions' => array("esl.esl_artistname LIKE"=>"%".$artistname."%"),
'limit' => $arrFind['limit'],
'page'=>$arrFind['page'] ))
*/
$this->find('all',condition);
how to do this? how to concatenate both conditions?
Why not initialize the conditions
array and just append to it?
$conditions = array();
if( keyword != '' ) {
array_push(
'conditions'
, array( "AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%", "esl.esl_songname LIKE"=>"%".$songname."%" ) )
}
if( !name != '' ) {
array_push( 'conditions', array("esl.esl_artistname LIKE"=>"%".$artistname."%")
}
$this->find( 'all', array( 'conditions' => $conditions, 'limit' => $arrFind['limit'], 'page' => $arrFind['page'];
I would do something like Rob Wilkerson suggested but like this:
$conditions = array();
if(keyword != '') {
$conditions[] = array( "AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%", "esl.esl_songname LIKE"=>"%".$songname."%" ) );
}
if(!name != '') {
$conditions[] = array("esl.esl_artistname LIKE"=>"%".$artistname."%");
}
$this->find( 'all', array( 'conditions' => $conditions, 'limit' => $arrFind['limit'], 'page' => $arrFind['page'];
Concatenating condition could be done in following way:
$condition1 = array('Model1.field1 LIKE'=>'%'.$value.'%', 'Model1.field2'=>2);
$condition2 = array('Model2.field2 LIKE'=>'%'.$value.'%', 'Model2.field2'=>1);
$this->MyModel->find('all', array('conditions'=>am($condition1, $condition2)));
am is the cake's shortcut to array_merge.
精彩评论