10

我对 Zend Framework 和 MVC 还很陌生,我对 Zend_DB 以及与数据库交互的正确方式有点困惑。

我正在使用 PDO MySQL 适配器并创建了一些类来扩展抽象类:

class Users extends Zend_Db_Table_Abstract {
    protected $_name = 'users';
    protected $_primary = 'user_id';
    protected $_rowClass = 'User';

    public function getUserbyID($id) { /* code */ }
    // More code here
}
class User extends Zend_Db_Table_Row_Abstract {
    // Code here
}
class Widgets extends Zend_Db_Table_Abstract {
    protected $_name = 'widgets';
    protected $_rowClass = 'Widget';

    public function getWidgetsfromUser($userid) { /* code */ }
    // More code here
}
class User extends Zend_Db_Table_Row_Abstract {
    public function doSomethingWithWidget() { /* code */ }
    // More code here
}

似乎有很多方法可以访问数据库(fetchAll()、find()、fetchAll() 通过适配器、insert()、createRow() 和 save()、select() 对象),我总是发现自己要回去让文档弄清楚我应该做什么。

SO 教会了我准备好的语句是要走的路,我一直在尝试使用行集和行(我应该是吗?),但我仍然对与数据库交互的最佳方式感到困惑?

(为这个非常开放的问题道歉)

4

3 回答 3

9

一般来说,人们更喜欢通过 Table 和 Row 对象来访问数据库,以符合他们面向对象编程的习惯。

如果您需要编写代码来转换或验证查询输入或输出,OO 方法很有用。您还可以在 Table 或 Row 类中编写自定义方法来封装经常需要的查询。

但是面向对象的接口被简化了,无法执行您可能需要执行的所有类型的数据库操作。query()因此,fetchAll()当您需要更好地控制 SQL 时,您可以更深入地研究并针对 Zend_Db_Adapter 方法运行 SQL 查询。

这对于数据库的面向对象接口非常常见。一个可以复制每个SQL 特性的 OO 层会非常复杂。因此,为了妥协,OO 层通常会尝试提供简单的方法来完成最常见的任务,同时让您能够在必要时进行隐藏。

这是对您非常普遍的问题的非常普遍的回答。

于 2009-01-15T02:37:26.257 回答
8

使用 Zend_Db,您可能不想了解准备好的语句等的细节。您只想使用模型对象来执行基本的 CRUD(创建、读取、更新和删除)。我知道Programmer's Reference Guide内容广泛,但它是对 Zend_Db 的一个很好的介绍。您可能需要仔细查看Zend_Db_Table文档。

但是要快速回答您的问题。除非您需要覆盖某些默认行为,否则您不需要扩展 Zend_Db_Table_Row_Abstract。您也可以将 Users 类简化为:

class Users extends Zend_Db_Table_Abstract {
  protected $_name = 'users';

  // Code here
}

然后要使用它,您将使用以下方法执行您提到的一些事情:

//Create a Users Model Object
$usersDb = new Users();

//Find the record with user_id = 4 and print out their name
$row = $usersDb->find(4);
echo $row->first_name . ' ' . $row->last_name

//Change the first name of this user to Brian    
$row->first_name = 'Brian';
$row->update();

//Insert a user into the database
$data = array(
  'first_name' => 'Gilean',
  'last_name' => 'Smith');
$usersDb->insert($data);

//Retrieve all users with the last name smith and print out their full names
$rows = $usersDb->fetchAll($usersDb->select()->where('last_name = ?', 'smith'));    
foreach ($rows as $row) {
  echo $row->first_name . ' ' . $row->last_name
}
于 2009-01-15T02:35:45.263 回答
4

我建议使用保存方法。

//Create a Users Model Object
$usersDb = new Users();

//Find the record with user_id = 4 and print out their name
$row = $usersDb->find(4);
echo $row->first_name . ' ' . $row->last_name

//Change the first name of this user to Brian    
$row->first_name = 'Brian';
$row->save();

//Insert a user into the database
$newUser = $usersDb->fetchNew();
$newUser->first_name = 'Gilean';
$newuser->last_name  = 'Smith';
$newUser->save();

// OR if you get your data from post or any array
$newUser = $usersDb->fetchNew();
$newUser->setFromArray($data_from_post);
$newUser->save();

我更喜欢这种方法的原因是因为这就是为什么你总是有一个用户模型的实例并且你可以在其中拥有自定义方法(例如 isAdmin),还因为你可能想要覆盖 userRow 上的保存/插入/更新功能在插入/更新之前做一些事情。

于 2009-03-05T22:38:11.120 回答