使用 PDO 创建模型层

发布于 2024-11-07 18:57:43 字数 778 浏览 3 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-11-14 18:57:43

您的基模型类不应继承自数据库访问类。相反,它应该使用数据库类(或映射器,谁说它必须始终是数据库?),然后提供 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

我爱人 2024-11-14 18:57:43

我见过的最接近的东西是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文