Models in the Zend Framework(Zend 框架中的模型)
问题描述
您在 Zend 框架中实现模型的方式有哪些?
What are some of the ways you have implemented models in the Zend Framework?
我已经看到了基本的 class User extends Zend_Db_Table_Abstract
然后在你的控制器中调用它:
I have seen the basic class User extends Zend_Db_Table_Abstract
and then putting calls to that in your controllers:
$foo = 新用户;
$foo->fetchAll()
但是更复杂的用途呢?文档的快速入门部分提供了这样一个示例,但我仍然觉得我没有得到 Zend Framework 中模型的最佳使用"示例.有什么有趣的实现吗?
but what about more sophisticated uses? The Quickstart section of the documentation offers such an example but I still feel like I'm not getting a "best use" example for models in Zend Framework. Any interesting implementations out there?
我应该澄清(回应 CMS 的评论)...我知道做更复杂的选择.我对模型概念的整体方法和其他人如何实现它们的具体示例感兴趣(基本上,手册遗漏的内容以及基本操作方法所掩盖的内容)
I should clarify (in response to CMS's comment)... I know about doing more complicated selects. I was interested in overall approaches to the Model concept and concrete examples of how others have implemented them (basically, the stuff the manual leaves out and the stuff that basic how-to's gloss over)
推荐答案
我个人对 Zend_Db_Table_Abstract
和 Zend_Db_Table_Row_Abstract
进行了子类化.我的代码和你的代码之间的主要区别在于,明确地将 Zend_Db_Table_Abstract
的子类视为表",将 Zend_Db_Table_Row_Abstract
的子类视为行".我很少在控制器中看到直接调用选择对象、SQL 或内置 ZF 数据库方法.我尝试隐藏请求特定记录以调用 Zend_Db_Table_Abstract
后面的逻辑,如下所示:
I personally subclass both Zend_Db_Table_Abstract
and Zend_Db_Table_Row_Abstract
. The main difference between my code and yours is that explicitly treat the subclass of Zend_Db_Table_Abstract
as a "table" and Zend_Db_Table_Row_Abstract
as "row". Very rarely do I see direct calls to select objects, SQL, or the built in ZF database methods in my controllers. I try to hide the logic of requesting specific records to calls for behind Zend_Db_Table_Abstract
like so:
class Users extends Zend_Db_Table_Abstract {
protected $_name = 'users';
protected $_rowClass = 'User'; // <== THIS IS REALLY HELPFUL
public function getById($id) {
// RETURNS ONE INSTANCE OF 'User'
}
public function getActiveUsers() {
// RETURNS MULTIPLE 'User' OBJECTS
}
}
class User extends Zend_Db_Table_Row_Abstract {
public function setPassword() {
// SET THE PASSWORD FOR A SINGLE ROW
}
}
/* CONTROLLER */
public function setPasswordAction() {
/* GET YOUR PARAMS */
$users = new Users();
$user = $users->getById($id);
$user->setPassword($password);
$user->save();
}
有很多方法可以解决这个问题.不要认为这是唯一的,但我尝试遵循 ZF 的设计意图.(这里有更多我的关于这个主题的想法和链接.)这种方法确实有点类很重,但我觉得它让控制器专注于处理输入和与视图协调;让模型去做特定于应用程序的工作.
There are numerous ways to approach this. Don't think this is the only one, but I try to follow the intent of the ZF's design. (Here are more of my thoughts and links on the subject.) This approach does get a little class heavy, but I feel it keeps the controllers focused on handling input and coordinating with the view; leaving the model to do the application specific work.
这篇关于Zend 框架中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Zend 框架中的模型
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01