$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