Zend框架:与数据库交互的正确方法? [英] Zend Framework: Proper way to interact with database?

查看:120
本文介绍了Zend框架:与数据库交互的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Zend框架和MVC相当新,我对Zend_DB和与数据库交互的正确方式感到困惑。



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

 类用户扩展Zend_Db_Table_Abstract {
protected $ _name ='users';
protected $ _primary ='user_id';
protected $ _rowClass ='User';

public function getUserbyID($ id){/ * code * /}
//更多代码在这里
}
类用户扩展Zend_Db_Table_Row_Abstract {
/ / code here
}
类Widgets扩展Zend_Db_Table_Abstract {
protected $ _name ='widgets';
protected $ _rowClass ='Widget';

public function getWidgetsfromUser($ userid){/ * code * /}
//这里有更多代码
}
类用户扩展Zend_Db_Table_Row_Abstract {
public function doSomethingWithWidget(){/ * code * /}
//这里有更多代码
}


$ b b

似乎有很多方法通过adapter,insert(),createRow()和save(),select()对象访问DB(fetchAll(),find(),fetchAll发现自己回去的文档,以找出我应该做的。



SO已经教我准备语句是要走的路,我一直在试图使用行集和行(我应该是?),但我仍然困惑与数据库交互的最佳方式是什么?

解决方案

使用Zend_Db你可能不想进入准备的细节语句等。你只是想使用模型对象来做基本的CRUD(创建,读取,更新和删除)。我知道程序员参考指南是广泛的,但它是一个伟大的介绍到Zend_Db。您可以仔细查看 Zend_Db_Table 文档。



但是给你一个快速的答案你的问题。除非你需要重写一些默认行为,你不需要扩展Zend_Db_Table_Row_Abstract。你还可以简化Users类只是:

 类用户扩展Zend_Db_Table_Abstract {
protected $ _name ='用户;

//代码在这里
}

您可以使用以下方法执行您提到的某些操作:

  //创建用户模型对象
$ usersDb =新用户();

//找到user_id = 4的记录并打印出他们的名字
$ row = $ usersDb-> find(4);
echo $ row-> first_name。 ''。 $ row-> last_name

//将此用户的名字更改为Brian
$ row-> first_name ='Brian';
$ row-> update();

//将用户插入数据库
$ data = array(
'first_name'=>'Gilean',
'last_name'=> Smith');
$ usersDb-> insert($ data);

//检索所有名为smith的用户并打印出他们的全名
$ rows = $ usersDb-> fetchAll($ usersDb-> select() - >其中('last_name =?','smith'));
foreach($ rows as $ row){
echo $ row-> first_name。 ''。 $ row-> last_name
}


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
}

这篇关于Zend框架:与数据库交互的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆