$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"
我希望这至少能给你指明方向。祝你好运!