Codeigniter 模型只是实用程序类?
在我习惯的 MVC 中,模型类(通常)表示表,这些类的对象是行/域对象。我不明白 CodeIgniter 中为什么模型类看起来只是单例实用程序类。感觉写得不对,
$data = array('text' => 'hello');
$this->commentModel->insert($data);
而不是
$comment = new Comment();
$comment->text = 'hello';
$comment->save();
有人能解释一下为什么 CodeIgniter 会这样建模并使我感觉更好吗? (或者告诉我我能做些什么来解决这个问题。)
In the MVC that I'm accustomed to, model classes (usually) represent tables and objects of these classes are rows/domain objects. I don't understand in CodeIgniter why model classes appear to just be singleton utility classes. It feels wrong writing
$data = array('text' => 'hello');
$this->commentModel->insert($data);
instead of
$comment = new Comment();
$comment->text = 'hello';
$comment->save();
Can someone explain why CodeIgniter does models this way and make me feel better about it? (Or tell me what I can do to fix it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CodeIgniter 中的模型是使用单例模式设计的,你是对的。虽然这对于许多习惯使用 PHP OOP 方法的人来说似乎很困惑,但有几个原因。
这是这里唯一真正的优点,其余的都是抱歉的解释。
现在 EllisLab 已从 CI 2.0 中删除了对 PHP 4 的支持,这种情况才刚刚开始改变,但就目前而言,这就是框架的工作方式。
当然,您可以加载模型,然后为模型使用您喜欢的任何 PHP 5 语法。
Models in CodeIgniter are designed using singleton pattern you're right. While this seems confusing to many people who are used to working with a more PHP OOP approach, there are a few reasons.
That's the only real plus here, the rest are apologetic explanations.
This is only just starting to change now EllisLab has dropped PHP 4 support from CI 2.0, but for now, that's how the framework works.
You can of course load a model then use whatever PHP 5 syntax you like for your models.
CodeIgniter 不包含 ORM。
我建议查看可以轻松与 CI 集成的 Doctrine:
http://codeigniter.com/wiki/Using_Doctrine_with_Code_Igniter/
CodeIgniter doesn't include an ORM.
I would suggest to see at Doctrine that can be easily integrated with CI:
http://codeigniter.com/wiki/Using_Doctrine_with_Code_Igniter/
您可以查看我关于如何将 Doctrine (ORM) 与 Codeigniter 结合使用的文章。
第 1 部分(共 11 部分):
http://www.phpandstuff.com /文章/codeigniter-doctrine-from-scratch-day-1-install-and-setup
You can check out my articles on how to use Doctrine (ORM) with Codeigniter.
Part 1 of 11:
http://www.phpandstuff.com/articles/codeigniter-doctrine-from-scratch-day-1-install-and-setup