开发者

Zend framework query, add to string

let's say that I have:

$query = $this->select()
              ->where('name LIKE ?','%something%');
              ->where('section LIKE ?','%something%');
              ->where('cars LIKE ?','%something%');
              ............
              ............

You get the point!

But I want to add the where() clause to the query only let's say if..$x = 0; something like this:

$select = $this->select();
if($x == 0):
//add to the $this->select(); so it will be $this->select()->where('section LIKE   ?','%something%');
 $select->where('section LIKE ?','%something%');
endif;

if($x == 0):
/*add to the $this->select(); so it will be 
$this->select()->where('section LIKE  ?','%something%')
->where('cars LIKE ?','%something%');
*/
 $select->where('cars LIKE ?','%something%');
endif;

Of cour开发者_StackOverflow中文版se, this thing doesn't work but I guess you have understood what I want to make!

Best Regards,


Have you considered building the SQL query string manualy beforehand and then just calling fetchAll() on your $db reference?

$SQL = 'SELECT * from blah, ';
if($x == 0):
    //add to the $this->select(); so it will be $this->select()->where('section LIKE   ?','%something%');
    $SQL = $SQL . "WHERE blah");
endif;

if($x == 1):
    $SQL = $SQL . "WHERE blah");
endif;

$db = Zend_Db_Table::getDefaultAdapter();
$this->view->leaguetable = $db->fetchAll($SQL);

The Zend_DB.select() returns a sql query statement

$select = $this->select();

but you can't conditionally control the addition of where clauses to the object after the select() method has finished. It's not an append operation.

$select = $this->select();
if($x == 0):
 $select->where('section LIKE ?','%something%');
endif;


Problem appears to be your if clause, instead of

$x = 0

you want

$x == 0

The former is assigning 0 to $x in the if clause, which is almost never what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜