Zend框架中的模型 [英] Models in the Zend Framework

查看:60
本文介绍了Zend框架中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您在Zend Framework中实现模型的方式有哪些?

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 = new User;

$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_AbstractZend_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框架中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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