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
.
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?
it's so simple!
suppose you have a Model Called Article. you will have three classes called BaseArticle.class.php and Article.class.php and ArticleTable.class.php
here is the definition for each class:
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 : 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 : 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();