在Symfony的教义ORM文件Model.class.php和ModelTable.class.php之间进行选择 [英] Choosing between Symfony's Doctrine ORM files Model.class.php and ModelTable.class.php

查看:82
本文介绍了在Symfony的教义ORM文件Model.class.php和ModelTable.class.php之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为表构建模型文件时,它会生成三个文件,本质上是BaseModel.class.phpModel.class.phpModelTable.class.php.常识要求对Model.class.phpModelTable.class.phpBaseModel.class.php进行任何修改.

when doctrine builds model files for a table, it generates three files, essentially BaseModel.class.php, Model.class.php and ModelTable.class.php. Common knowledge requires that any modifications be done to Model.class.php or ModelTable.class.php vs BaseModel.class.php.

但是什么时候在Model.class.phpModelTable.class.php之间选择.据我了解,Model.class.php用于单个实例,而ModelTable.class.php用于多个实例.

But when do you choose between either Model.class.php vs ModelTable.class.php. From what I gather is that Model.class.php is for a single instance and ModelTable.class.php is for multiple instances.

任何人都可以在这里照明吗?

Can anyone shed any light here?

推荐答案

非常简单!

假设您有一个模型称为文章.您将拥有三个名为 BaseArticle.class.php Article.class.php ArticleTable.class.php

suppose you have a Model Called Article. you will have three classes called BaseArticle.class.php and Article.class.php and ArticleTable.class.php

这是每个类的定义:

BaseArticle.class.php :此类具有模型定义(或表定义).您不想编辑此类!

BaseArticle.class.php : this class has your model definition(or your table definition). you don't want to edit this class Ever!

Article.class.php :是您可以为模型编写的覆盖方法的地方.只有拥有Article Class实例时,您才可以访问这些功能. 因此您只能通过对象调用它们.例如:

Article.class.php : is a place for your override methods that you can write for your models. you only have access to these functions when you have an instance of Article Class. so you can call them only by an object. for example:

class Album extends BaseArticle {

    public function getSummary(){
         ....
    }

}

要使用它,您应该这样做:

for using it you should do this:

$article=new Article();
$article->getSummary();

ArticleTable.class.php :这是您要编写适用于整个Article talbe(或者最好是针对整个Article模型的函数)的地方. 例如,如果您想查找最受欢迎的文章,则不能将此函数编写到Article Class中,因为它仅适用于对象.但您想在整个表上执行此操作.所以:

ArticleTable.class.php : and this is where you want to write your functions that are for your whole Article talbe(or it's better to say for your whole Article model). for examlpe you want to find most popular articles, you can't write this function to your Article Class because it works only on an object. but you want to do this on your whole Table. so:

class AlbumTable extends Doctrine_Table {
    public static function getPopularArticles() {
    }
}

,如果您的函数是静态的,则必须像这样使用它:

and you have to use it like this if your functions are static:

ArticleTable::getPopularArticles();

,如果您的函数不是静态的,则可以使用以下命令调用它们:

and if your functions are not static you can call them using:

Doctrine_Core::getTable('Product')->your_nonstatic_function();

这篇关于在Symfony的教义ORM文件Model.class.php和ModelTable.class.php之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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