views:

314

answers:

2

Hi guys, 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 course, this thing doesn't work but I guess you have understood what I want to make!

Best Regards,

A: 

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.

Alan Storm
srry, that was a tipo..but still doesn't do what I want to do...it needs to create the query format for me
Uffo
+1  A: 

Hey,

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;
emeraldjava
Actually, you can use that code $select->where() to add more where operations.
Chris
yeah I know, thats what I did, I had a bug..and thats why didn't work!
Uffo