Zend简单视图控制器模型指南


Zend Simple View-Controller-Model Guidelines

我是Zend框架的新手,所以请耐心等待。

我让一个控制器与一个模型交互,然后将该信息发送到视图。目前我的代码看起来像这样:

//Controller
$mapper = new Application_Model_Mapper();
$mapper->getUserById($userID);      
$this->view->assign('user_name', $mapper->user_name);
$this->view->assign('about', $mapper->about;
$this->view->assign('location', $mapper->location);
//Model
class Application_Model_Mapper
{
    private $database;
    public $user_name;
    public $about;
    public $location;
public function __construct()
{
    $db = new Application_Model_Dbinit;
        $this->database = $db->connect;
}
public function getUserById($id)
{
    $row = $this->database->fetchRow('SELECT * FROM my_table WHERE user_id = '. $id .'');
    $this->user_name = $row['user_name'];
    $this->about = $row['about'];
    $this->location = $row['location'];
}
}
//View
<td><?php echo $this->escape($this->user_name); ?> </td>
<td><?php echo $this->escape($this->about); ?></td>
<td><?php echo $this->escape($this->location); ?></td>

这段代码显然不是完整的,但你可以想象我是如何使用模型的。我想知道这是否是一个好的Zend编码策略?

我想知道,因为如果我有更多的数据要从模型中提取,控制器开始变得相当大(每个项目一行),并且模型有很多公共数据成员。

我忍不住认为有更好的方法,但我正在努力避免让视图直接访问模型。

提前谢谢!

请ZF团队负责人查看这些关于对象建模的幻灯片。

http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects

您应该处理完整的对象,而不是按属性分解和重建它们。

Zend有一个DB抽象层,您可以使用它来快速处理它

http://framework.zend.com/manual/en/zend.db.htmlhttp://framework.zend.com/manual/en/zend.db.table.html

作为起点,开始向视图传递完整的(首选的数据传输)对象。

//This is just a simple example, I'll leave it up to you how you want to organize your models. You can use several strategies. At work we use the DAO pattern. 
$user = $userModel->getUser($id);
$this->view->user  = $user;
And in your view,
Name : <?=$this->user->name?> <br>
About me : <?=$this->user->about?> <br>