使用 PDO 创建模型层
我目前正在开发一个 MVC 应用程序框架,我来寻求一些关于如何构建模型层的建议。
构建模型时,每个模型都映射到该应用程序数据库中的一个表,因此典型的应用程序将具有
- 配置
- 主题
- 论坛
,并且每个模型都将映射到名为 PHP 文件,例如 app/models/configuration。 ,
现在我遇到的问题是创建父数据库类以能够处理特定的表数据,例如:
class PDOModel
{
public function __construct()
{
$this->__Communicator = Registry::getPDOInstance();
}
public function getSingle($id)
{
return /*Row*/;
}
/*Etc*/
}
然后对于应用程序模型进行类似的操作
class Model_Topic extends PDOModel
{
protected $__id_column = 'id';
}
然后在我的控制器中我可以像这样使用:
$Topic = $this->model->topic->get(22);
但是我还想考虑自动连接表,是否有任何简单的轻量级库已经过测试并符合我的要求。
任何帮助将不胜感激。
Im currently developing a MVC application Framework and I have come for some advise in regards to the way I should construct my Model Layers.
The model is constructed so that each model is mapped to a table within the database for that applications, so a typical application would have
- Configuration
- Topics
- Forums
and each would be mapped to there named PHP file such as app/models/configuration.php
Now the problem I am having is creating the parent database class to be able handle specific table data, for example:
class PDOModel
{
public function __construct()
{
$this->__Communicator = Registry::getPDOInstance();
}
public function getSingle($id)
{
return /*Row*/;
}
/*Etc*/
}
And then something like this for the application model's
class Model_Topic extends PDOModel
{
protected $__id_column = 'id';
}
and then within my controller I can use like so:
$Topic = $this->model->topic->get(22);
But i would also like to take into considerations auto joining tables as well, are there any simple lightweight libraries about that have been tested and fit the my requirements.
Any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的基模型类不应继承自数据库访问类。相反,它应该使用数据库类(或映射器,谁说它必须始终是数据库?),然后提供 ORM 方法(获取、插入、更新等)。
正如其他人所发布的,您应该使用许多优秀的预构建解决方案之一。至于寻找一个轻量级的 ORM,其他人之前已经问过这个问题。以下是这些问题之一的链接:
https://stackoverflow.com/questions/1995834/looking -a-轻量级-php-orm
Your base model class shouldn't inherit from the database access class. Rather, it should use the database class (or mapper, who says it must always be a database?) and then provide the ORM methods (get, insert, update, etc...).
As others have posted, you should use one of the many fine pre-built solutions. As far as looking for a lightweight ORM, others have asked this before. Here's a link to one of those questions:
https://stackoverflow.com/questions/1995834/looking-a-lightweight-php-orm
我见过的最接近的东西是 Zend_Db,它在 PDO 之上实现了表和行网关。它支持连接相关表等。
通常,您要求的并不是那么“轻量级”,而是某种完整的 ORM。就我个人而言,如果我是你,我会选择一个像样的 ORM。 Propel >= 1.5、Doctrine 1.2 或 Doctrine2。
The closest thing ive seen is Zend_Db which implements Table and Row gateways on top of PDO. It has support for joining related tables and what not.
Typically what youre asking for isnt going to be all that "light weight", its going to be a full ORM of sorts. Personally if i were you i would just go with a decent ORM. Propel >= 1.5, Doctrine 1.2, or Doctrine2.