【发布时间】:2013-05-14 11:51:00
【问题描述】:
我对如何构建模型感到很困惑,在过去的 9 个月里我一直无法理解这一点。虽然我正在阅读和观看所有参考资料,但@teresko 给了我。
为了进一步缩小我的问题,我将举例说明我以前是如何做到的。
假设我有一个student 实体,它具有student_number、first_name,last_name`
然后我将创建我所谓的model,(我的教授也这样做了,但我知道这是完全错误的)。我不知道我的教授是否知道值对象,但我知道。
private $student_number;
private $first_name;
private $last_name;
public function setStudentNumber($sn) {$this->student_number = $sn}
public function getStudentNumber() {return $this->student_number}
... and so on for other properties
如果我是正确的,这个 setter 和 getter 被归类为值对象模式,可以这样使用:
$s = new Student();
$s->setStudentNumber(143);
$s->setFirstName('FooName');
$s->setLastName('BarName');
然后像这样在数据访问对象 (StudentDAO) 中传递它:
$sDao = new StudentDAO($s);
$sDao->add();
DAO 扩展了数据库类,所以我可以做 CRUD 之类的。
问题是,好吧,我很确定我会在这里受到很多责骂,因为我错过了太多原则,但那些是什么?如何创建我的模型?谢谢!好吧,我知道很多答案都会涉及 DataMappers、Factories 和其他东西,我不太了解。
【问题讨论】:
-
来自 Zend 页面定义的模型:
Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model. -
有很多方法可以抽象出数据访问。阅读 this 帖子并点击指向 martinfowler 博客的链接。
-
可能重复:stackoverflow.com/questions/5863870/… 最佳答案包含标题 什么不是模型? 和 什么是模型?
-
另见thin controller fat model。所以你的模型应该包含业务逻辑而不是控制器。
标签: php model-view-controller architecture models controllers