Suggestions( About DAO )

Suggestions( About DAO )

Postby asers on Fri Feb 22, 2008 9:25 pm

Code: Select all
<?php

// I am not good at English... Read scripts -> under here ...
//
// About : modify -> DAO classes

/**
 * LifeType 中 Article 对象的构造建议,没有深入地了解 LifeType 的代码
 * 只是提出一个方案建议,大家可讨论。
 *
 * 时间不多,代码并没有写完整,只是表达了自己的想法,有兴趣的可尝试改
 * 造,来验证它的可行性。
 *
 * Author : Zhang Shiwei (asers_z@hotmail.com)
 * 2008-2-23
 */
class Article
{
   
   /**
    * 用来存储文章列表的数组,无论一篇或多篇
    *
    * @private Array
    */
   private $_articles = array();

   /**
    * 存储此次循环时获得的文章资料
    *
    * @private Array && Default FALSE
    */
   private $_article = false;

   /**
    * 存储此次循环中获得的数组索引
    *
    * @private Int
    */
   private $_subScript = false;


   /**
    * 构造函数
    *
    * @public
    */
   public function __construct( $topic, $text, $categories, $user, $blog, $status, $numReads, $properties = Array(), $slug = "", $id = -1 )
   {
      // LifeType 的具体实现
   }

   /**
    * 将获得的文章压入数组,执行LifeType中类似 getBlogArticles 方法时,循环压入
    *
    * @param : result 获得单条数据库记录数组
    * @return : true
    */
   public function pushArticle( $result )
   {
      // 如果 _article 成员为 false,意为该对象压入第一条记录,则记录在这个成员上
      // 并将压入数组
      //
      // 记录第一条的原因是系统并不知道此时压入的是文章列表或单条文章,记录在这里
      // 后可以确保如果是单条文章,下面的各个方法仍然可以正常取值
      //
      // 当然也可以通过构造函数来确认此时压入的是单篇文章而不是列表
      if($this->_article === false) {
         $this->_article = $result;
         $this->_articles[] = $this->_article;
      }

      else $this->_articles[] = $result;
   }

   /**
    * 在其他位置需要对列表进行读取时使用 while 句柄循环该方法
    *
    * @public
    */
   public function listArticles()
   {
      return list($this->_subScript, $this->_article) = each($this->_articles);
   }

   /**
    * 获得文章的标题
    *
    * @public
    */
   public function getTopic()
   {
      return $this->_article["topic"];
   }

   /**
    * 获得文章的介绍内容
    *
    * @public
    */
   public function getIntroText()
   {
      return $this->_article["introtext"];
   }

   // ...... 更多的相关方法的实现不再一一列表

}



class Articles extends Model
{
   // ........... //

   function getBlogArticles( $blogId,
                       $date         = -1,
                       $amount       = -1,
                       $categoryId   = 0,
                       $status       = POST_STATUS_PUBLISHED,
                       $userId       = 0,
                       $maxDate      = 0,
                       $searchTerms  = "",
                       $page         = -1 )
   {
      // Modify !!!!!!!!!!!!!!!!! ........................................................ //
      $article = new Article( ... );

      while( $row = $result->FetchRow()) {
         // Push article
         $article->pushArticle( $row );
         // and cache it for later use, we might need it
         $this->_cache->setData( $article->getId(), CACHE_ARTICLES, $article );
         $this->_cache->setMultipleData( $article->getPostSlug(), CACHE_ARTICLES_BYNAME, $article );
      }

      $result->Close();

      return $article;
   }

   // .......................................................................... //
}


// List articles ....................................................... //
$article = new Article( ... );
while( $article->listArticles() ) {
   // ......... //
   // ....
}
// ..................................................................... //
asers
 
Posts: 3
Joined: Fri Feb 22, 2008 5:25 pm
Location: Jianwai SOHO, East 2 ring Rd, BeiJing , China

Postby jondaley on Sun Feb 24, 2008 1:50 am

I am not clear on what you are saying - you modified a couple files, and think your modifications should be included in the core code? What does your code do that is different from the default?

If you use subversion, can you run an "svn diff XXX.class.php", or if you don't use subversion can you diff your file with the original, so I can see what you changed?
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Postby asers on Sun Feb 24, 2008 3:55 am

I was very surprised I can understand your words !!!

You read very carefully that code?

You put 15 Objects inside Array, this consumes a lot of memory.


In my code :

15 result has been put on a database object, instead of 15 objects has been put on an array

Code: Select all
 while( $row = $result->FetchRow()) {
         // Push article
         $article->pushArticle( $row );
         ......
}



In a cycle of this method($article->listArticles()) of recycling..

Code: Select all
while( $article->listArticles() ) {
   $article->getTopic();
   $article->getContent();
   // ....
}


Do you understand?
--------------------------------------------------
Re-read the top of the page code and compare the different you can understand.
asers
 
Posts: 3
Joined: Fri Feb 22, 2008 5:25 pm
Location: Jianwai SOHO, East 2 ring Rd, BeiJing , China


Return to LifeType 2.0 Development

cron