I’m learning Zend Framework and I’m working through a Database inconvenience.
Using php you could do some string manipulation like this
$sqlstatement = "select id,name from TABLE where ID='$id'";
if ($admin == true) {
$sqlstatement .= " and admin = 'Yes'";
}
Is there a way to do this with Zend? currently I am doing something like this.
if ($admin == true){
$sqlstatement = $this->select()
->from($this->_name,array('id','name'))
->where("ID='$id'")
->where("admin = 'Yes'");
}else{
$sqlstatement = $this->select()
->from($this->_name,array('id','name'))
->where("ID='$id'");
}
Ideally I would like to do something like…
$sqlstatement = $this->select()
->from($this->_name,array('id','name'))
->where("ID='$id'");
if ($admin == true){
$sqlstatement .= $this->select()
->where("admin = 'Yes'");
}
I have a Model creating the queries and returning them to my view. So the $this->select() is actually happening in my model class function and returning the Zend_Db_Table_Row Object.
Yes,
select()returns the select object which you can call methods on as many times as you wish, so:also you should really use parameterised queries to avoid SQL injection, so I would rewrite this code as: