MVC-由其他几个模型组成的模型可以吗? [英] MVC - Is it okay for a Model to be composed of a few other models?

查看:80
本文介绍了MVC-由其他几个模型组成的模型可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器:一个用于查看单个玩家,另一个用于查看团队.我目前在团队由一群玩家模型组成的地方拥有它.

I have two Controllers: one for viewing a single player, and one for viewing a team. I currently have it where the team is composed of a bunch of Player Models.

我是MVC的新手,在我阅读的所有内容中,我对由其他模型组成的模型了解不多.有没有其他方法可以解决这种情况,或者这看起来像是一个非常标准的实现?

I'm new to MVC and out of everything I've read, I haven't seen much on Models being composed of other Models. Is there an alternative way to approach this situation, or does this seem like a pretty standard implementation?

推荐答案

模型不是事物"或类".这是一层.

Model is not a "thing" or a "class". It is a layer.

模型层由多个元素组成.两个最重要的类型是域对象  [1]   [2] 数据访问对象(通常实现为 DataMappers   [2] 模式).第三类结构是服务,但我将尝试使其保持简单.

And model layer is made up from multiple elements. Two of most important types are Domain Objects [1] [2] and Data Access Object ( usually implemented as DataMappers [1] [2] pattern). The third type of structures would be services, but i'm gonna try to keep it simple.

域对象是业务逻辑与模型层一起使用的地方.而且不仅可能使域对象包含其他域对象,而且通常这也是最好的方法.这是一个小例子:

The domain objects is where the business logic is withing the model layer. And not only it is possible to have domain object containing other domain objects, it is usually the best way to to go. Here is a small example:

$group = $this->modelFactory->buildObject('group');
$mapper = $this->modelFactory->buildMapper('group');

$group->setName('wheel');
$mapper->fetch($group);

$user = $this->modelFactory->buildObject('user');
$user->setName('foobar');
$user->setHomeDir('/home/user');
$user->setShell('/bin/csh');

$group->addUser( $user );
$mapper->store($group);

阅读本主题,您可能还会受益.

You also might benefit from reading this topic.

这篇关于MVC-由其他几个模型组成的模型可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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