Zend Framework:与数据库交互的正确方法?

发布于 2024-07-11 16:33:26 字数 1007 浏览 10 评论 0原文

我对 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教会了我准备好的语句是正确的方法,我一直在尝试使用行集和行(我应该吗?),但我仍然很困惑与数据库交互的最佳方式是什么?

(对于这个极其开放式的问题表示歉意)

I'm fairly new to the Zend Framework and MVC and I'm a bit confused by Zend_DB and the proper way to interact with the database.

I'm using the PDO MySQL adapter and have created some classes to extend the abstract classes:

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
}

There seems to be so many ways to access the DB (fetchAll(), find(), fetchAll() through adapter, insert(), createRow() and save(), select() object) that I always find myself going back to the docs to figure out what I should be doing.

SO has taught me prepared statements are the way to go, and I've been trying to use rowsets and row (should I be?), but I'm still confused as to what's the best way to interact with the database?

(apologies for the terribly open ended question)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-07-18 16:33:26

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

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

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

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

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

In general, people prefer to access the database through the Table and Row objects, to match their habits of object-oriented programming.

The OO approach is useful if you need to write code to transform or validate query inputs or outputs. You can also write custom methods in a Table or Row class to encapsulate frequently-needed queries.

But the object-oriented interface is simplified, unable to perform all the types of database operations you might need to do. So you can delve deeper and run a SQL query against the Zend_Db_Adapter methods like query() and fetchAll() when you require finer control over your SQL.

This is pretty common for object-oriented interfaces to databases. An OO layer that could duplicate every SQL feature would be insanely complex. So to compromise, an OO layer usually tries to provide easy ways to do the most common tasks, while giving you the ability to go under the covers when necessary.

That's a very general answer to your very general question.

折戟 2024-07-18 16:33:26

使用 Zend_Db 您可能不想了解准备好的语句等的细节。 您只想使用模型对象执行基本的 CRUD(创建、读取、更新和删除)。 我知道程序员参考指南内容很丰富,但它是一个很棒的介绍到 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
}

Using Zend_Db you probably don't want to get into the details of prepared statements and the like. You just want to use the model objects to do basic CRUD (Create, Read, Update and Delete). I know the Programmer's Reference Guide is extensive, but its a great introduction to Zend_Db. You may want to take a closer look at the Zend_Db_Table documentation.

But to give a quick answer to your question. Unless you need to override some default behavior you shouldn't need to extend the Zend_Db_Table_Row_Abstract. Also you can probably simplify the Users class to just be:

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

  // Code here
}

Then to use it you would do some of things you mentioned using the following:

//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
}
め七分饶幸 2024-07-18 16:33:26

我建议使用保存方法。

//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 上的保存/插入/更新函数在插入/更新之前执行某些操作。

I recommend using the save method.

//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();

the reason why i like this approach more is because this why you always have an instance of the user model and you can have custom methods in them ( ex isAdmin ) and also because you might want to ovewrite the save/insert/update function on userRow to do something before they are inserted/updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文