Doctrine_Table Vs class从BaseClass扩展 [英] Doctrine_Table Vs class That extends from BaseClass

查看:142
本文介绍了Doctrine_Table Vs class从BaseClass扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个教义模式概念中有些困惑,让我们说一个名为文章
的表格教条将使用Zend框架和Doctrine 1.2生成名为
i的类

I am a little confused in this Doctrine model concept , lets say we a table called "article" Doctrine will generate class called i am using Zend framework and Doctrine 1.2


  1. models / generated / BaseArticle.php

  2. models / ArticleTable.php

  3. models / Article.php

在...中调用 ArticleTable 控制器以这种方式

Is it true to call the ArticleTable in the controller in this way

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

然后将其保存在这样的文章对象中

then to save it in the Article Object like this

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

我必须让 Article.php 调用 ArticleTable
然后在控制器中启动一个 Article.php 对象?

Or I have to let the Article.php to call the ArticleTable ? then to initiate an Article.php object in the controller ?

class Article extends BaseArticle
{

      public function getFArticles()
      {
           $tableArticle = Doctrine::getTable('Article');
           $obj =  $tableArticle->getFeaturedArticles();
          return $obj;
      }


推荐答案

Article.php不应该调用ArticleTable.php除非真的,真的需要。在表类中,您只会保留由控制器调用的查询,如:

Article.php should not call ArticleTable.php unless really, really needed. In table classes you will only hold queries called by controller like:

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

以上代码更简单,您可以在任何IDE中自动完成。

Above code is simpler and you will have autocompletion in any IDE.

在Article.php中不调用查询的原因是,您将能够轻松地过渡到Doctrine2一天。

The reason not to call queries in Article.php is that you will have easier transition to Doctrine2 one day.

对于表调用tbl_article或只是文章,学说会生成Article.php和BaseArticle.php。基本类不能手动更改。

For a table call tbl_article or just article, doctrine will generate Article.php and BaseArticle.php. Base classes must not be changed manually.

文章类是您的逻辑。例如,您在数据库中获取所有文章的列表。当您显示它们时,您希望功能文章拥有明星(仅举一个例子):

Article class is where your logic goes. For example, you fetch list of ALL articles in database. When you display them, you want feature articles to have a star (just an example):

控制器:

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

模板(Smarty版本):

template (Smarty version here):

{foreach $allArticles as $article}

  {if $article->isFeatured()}  <img src=.. some image ..>{/if}

  <h5>{$article->title}

{/foreach}

和模型类

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 usefull.

你真的试图用这个fromArray($ fArticle)做什么?我没有看到任何代码。

And what are you actually trying to do with this fromArray($fArticle)? I don't see any point of that code.

这篇关于Doctrine_Table Vs class从BaseClass扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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