The Doctrine_Table Vs class, which extends from BaseClass

I am a little confused in this concept of the Doctrine model, let's say we have a table called an “article” Doctrine will generate a class called I use the Zend and Doctrine 1.2 structure

  • models / generated / BaseArticle.php
  • models /ArticleTable.php
  • models /article.php

Is it correct to call ArticleTable in the controller.

$tableArticle = Doctrine::getTable('Article'); 

then save it in an article object like this

  $article = new Article(); $fArticles = $tableArticle->getFeaturedArticles(); foreach ($fArticles as $fArticle) { $article->fromArray($fArticle); echo $article->title } 

Or should I let Article.php call ArticleTable ? then run the Article.php object in the controller?

 class Article extends BaseArticle { public function getFArticles() { $tableArticle = Doctrine::getTable('Article'); $obj = $tableArticle->getFeaturedArticles(); return $obj; } 
0
source share
1 answer

Article.php should not call ArticleTable.php, if really, really . In table classes, you will only store queries that are called by the controller, for example:

 $featuredArticles = ArticleTable::getInstance()->getFeatured() ; 

The above code is simpler and you will have autocomplete in any IDE.

The reason for not responding to Article.php requests is that it will be easier for you to upgrade to Doctrine2 one day.

To invoke a tbl_article table or just an article, doctrine will generate the article. php and BaseArticle.php. Base classes should not be changed manually.

The article class is your logic. For example, you get a list of ALL articles in the database. When you show them, you want a star in art articles (just an example):

controller:

 $allArticles = ArticleTable::getInstance()->findAll() ; 

(Smarty version here):

 {foreach $allArticles as $article} {if $article->isFeatured()} <img src=.. some image ..>{/if} <h5>{$article->title} {/foreach} 

and model class

 class Article extends BaseArticle { const STATUS_FEATURED = 1 ; public function isFeatured() { return $this->status == self::STATUS_FEATURED ; } } 

All these are just some examples, in real life it is much more useful.

And what are you really trying to do with this fromArray ($ fArticle)? I do not see the point in this code.

+1
source

Source: https://habr.com/ru/post/920743/


All Articles