Zend Framework。与数据库互动的正确方式?[英] Zend Framework: Proper way to interact with database?

本文是小编为大家收集整理的关于Zend Framework。与数据库互动的正确方式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是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
}

似乎有很多方法可以访问db(fetchall(),find(),fetchall(),fetchall()通过适配器,insert(),createrow()和save(),select(),select()对象)我自己回到文档,弄清楚我应该做什么.

所以教我准备好的陈述是必经之路的,我一直在尝试使用系列和行(我应该是吗?),但是我仍然感到困惑互动的最佳方法是什么使用数据库?

(对这个非常开放的问题表示歉意)

推荐答案

使用zend_db,您可能不想介绍准备好的语句等的详细信息.您只想使用模型对象来执行基本的CRUD(创建,读取,更新和删除).我知道程序员的参考指南很广泛,但它是一个很好的介绍,但它是一个很好的介绍到zend_db.您可能需要仔细研究一下 zend_db_tb_table 文档.

,但要快速回答您的问题.除非您需要覆盖一些默认行为,否则您不需要扩展zend_db_table_row_abstract.另外,您可能可以将用户类简化为:

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
}

其他推荐答案

通常,人们更喜欢通过表和行对象访问数据库,以匹配其面向对象的编程的习惯.

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

但是,以对象为导向的接口被简化,无法执行您可能需要执行的所有数据库操作类型.因此,您可以更深入地研究并在需要对SQL的Zend_DB_Adapter方法上运行SQL查询.query()和fetchAll()

这对于面向对象的接口到数据库非常常见.可以复制每个 SQL功能的OO层将非常复杂.因此,为了妥协,OO层通常试图提供简单的方法来执行最常见的任务,同时使您能够在必要时进入封面.

这是对您非常普遍的问题的一个非常普遍的答案.

其他推荐答案

我建议使用保存方法.

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

我更喜欢这种方法的原因是因为这是为什么您始终具有用户模型的实例,并且您可以在其中具有自定义方法(ex iSadmin),也可以是因为您可能需要ovewrite overwite save/insert/update在userrow上功能在插入/更新之前做某事.

本文地址:https://www.itbaoku.cn/post/597564.html

问题描述

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)

推荐答案

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
}

其他推荐答案

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.

其他推荐答案

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.