在zendframework中使用Db类时,框架会自动给sql语句添加引号以防止数据库攻击 ,这就导致了一个问题,用户无法使用zend db类使用mysql的内置函数(方法,存储过程等)。好在zend框架提供了一个类Zend_Db_Expr,此类的构造函数会告诉框架不要对它所转化的类型进行添加引号的操作。
public function setBatchDelete($orderIds) {
$flag = YCL_Order::FLAG_DELETED;
$updateTime = time();
$data = array(
'flag' => new Zend_Db_Expr("flag | $flag"),
'update_time' => $updateTime
);
$db = $this->getAdapter();
$where = $db->quoteInto('service_order_id IN (?)',$orderIds);
$this->update($data,$where);
}
上面的例子 左边的flag是数据库中的flag。
$select=$db->select();
$select->from("testtable","*");
$select->where($db->quotInto("date>=?",new Zend_Db_Expr("UNIX_TIMESTAMP()"));
$db->fetchAll($select);
框架不会对UNIX_TIMESTAMP()添加引号,从而让用户能正常使用此函数。
You also can create an object of type Zend_Db_Expr explicitly, to prevent a string from being treated as a column name.