$this->db->fetchAll("SELECT * FROM TABLE");
實際上很容易完成。
我打算假設您已經在application.ini或bootstrap中設置了數據庫適配器。PHP只要你在某個地方定義了一個適配器,那它真的沒關係。
//simplified for demonstration
class SomeController extends Zend_Controller_Action
protected $db;
//items that are put into init() or predispatch() or similar function
//can usually be put into a frontcontroller plugin if needed app wide.
public function init() {
//this works as long as you have only one adapter defined or have set one as default.
$this->db = Zend_Db_Table::getDefaultAdapter();
}
public function indexAction() {
//with your db adapter defined you have access to the api for Zend_Db_Table_Abstract
$result = $this->db->fetchRow($sql);
//while a simple string will work as $sql I would
//recommend the select() be used if for not other reason then that
//the queries are built programatically
$select = $this->db->select();
$select->where('id = ?', $id)->orWhere('role = ?', 1); //selects can be chained or separated
$select->order('id','ASC');
$rows = $this->db->fetchAll($select);//returns rowset object, toArray() if needed.
}
}
我認爲最大的問題大多數人首先使用Zend_Db是搞清楚這是使用它的最佳方式時有。有很多方法可以使用這個組件,並且我們大多數人都會找到我們喜歡的一個,並將其用於一切。
例如一個簡單的SQL查詢可以表示爲一個字符串$sql = "SELECT * FROM TABLE";
作爲select() object如在我的例子的代碼。
您可以使用Zend_Db_Expr表示更復雜的表達式$select->where(new Zend_Db_Expr("FIND_IN_SET('$genre', genre)"));
不要忘記Zend_Db_Statement(這是我從來沒有使用過,所以不能正確地演示)。
Quoting of values and identifiers可根據需要使用。
[編輯]
因爲已經如前所述,你必須定義一個database adpater。我更喜歡在我的application.ini中執行此操作,但是Bootstrap.php也是定義適配器的常用位置。
在你的application.ini確保你至少有這行:
resources.db.adapter = "pdo_Mysql" //or your chosen adapter
resources.db.params.username = "username"
resources.db.params.password = "password"
resources.db.params.dbname = "dbname"
我希望這至少給你方向看好運!
強烈推薦閱讀此。 http://survivethedeepend.com/zendframeworkbook/en/1.0 – ficuscr