Zend_Db_Expr,zenddbtable

來源:互聯網
上載者:User

Zend_Db_Expr,zenddbtable

在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 typeZend_Db_Expr explicitly, to prevent a string from being treated as a column name.


zend framework184中怎輸出SQL語句 - PHP架構開發

親,1.8.4很老了,最新的可能是1.12.x吧(不考慮zf2),建議你升級到1.11或者1.12,應該不需要改動你的源碼。
對於你的問題,你這種用法是不需要輸出sql的,因為這條sql是你手動定義的,所以你完全可以自己輸出它。例如
$sql = "select count(*) from gs_user where username = '{$userName}' AND password = '{$password}'";echo $sql;print_r($this->db->fetchOne($sql));樓上的解答其實也是正確的,不過代碼不大完整,給你個例子
$select = new Zend_Db_Table_Select()$select->from('gs_users', array(new Zend_Db_Expr('COUNT(*)');$select->where('username', $userName)->where('password', $password);try{$row = $select->query(PDO::FETCH_ASSOC)->fetch();} catch(Exception $e) { die($select);}關於Zend Framework,這裡提供了一個可用的項目源碼,也許你給你點協助。www.941172.com/forum/view/baseproject源碼下載與配置運行

 
Zend Framework更新時怎不自動加引號 - PHP架構開發

如果你要更新的值,不是字串,而是一個SQL文法的話,需要用Zend_Db_Expr聲明下$data[\'val\'] = new Zend_Db_Expr(\'val + 1\'); 查看原帖>>

希望採納
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.