[pLog-svn] r2091 - plog/branches/plog-1.1-ben/class/dao

Allan Sun sunajia at gmail.com
Tue May 31 12:24:37 GMT 2005


> 
> 
> Regarding this one huge method, I didn't see any other option because
> there are many different possible combinations of parameters. You
> created a getBlogArticlesByCategory, getBlogArticlesByDate,
> getBlogArticlesByDateAndCategory and so on, but how about a
> getBlogArticlesByDateAndCategoryAndStatusAndUser? Or a
> getBlogArticlesByDateAndUser? If you for example look at the
> administration page, under "edit posts", you will see that you've got
> plenty of filters... If we need a method for each one of those, we
> could go mental :)))

 For the cases meeting the samiliar problems, I would change this into a 
search function immediately instead of using loads of similar functions:
  
> function search($params){
> include_once( PLOG_CLASS_PATH."class/dao/userinfos.class.php" );
> $UserInfos = new UserInfos();
> unset($params["action"]);
> if(!$params["info"])
> $params["info"]=0;
> if(!$params["mobileNumber"])
> $params["mobileNumber"]=0;
> 
> //$query = "SELECT * FROM vwUserInfo WHERE ";
> if($params["title"])
> $query .="userTitle LIKE ''%".$params["title"]."%'' AND ";
> if($params["gender"])
> $query .="gender LIKE ''%".$params["gender"]."%'' AND ";
> if($params["firstName"])
> $query .="firstName LIKE ''%".$params["firstName"]."%'' AND ";
> if($params["lastName"])
> $query .="lastName LIKE ''%".$params["lastName"]."%'' AND ";
> if($params["minAge"])
> $query .="minAge LIKE ''%".$params["minAge"]."%'' AND ";
> if($params["maxAge"])
> $query .="maxAge LIKE ''%".$params["maxAge"]."%'' AND ";
> if($params["city"])
> $query .="city LIKE ''%".$params["city"]."%'' AND ";
> if($params["postcode"])
> $query .="postcode LIKE ''%".$params["postcode"]."%'' AND ";
> if($params["info"] != "false")
> $query .="info = ''1'' AND ";
> if($params["mobileNumber"] != "false")
> $query .="mobileNumber IS NOT NULL ";
> if(substr($query,strlen($query)-4,strlen($query))== "AND ")
> $query = substr($query,0, strlen($query)-4);
> 
> //echo $query;
> //echo substr($query,strlen($query)-4,strlen($query));
> $query = "EXEC sp_searchUserGroup ' ".$query."'";
> $userInfos = $this->_db->EXECUTE($query)->GetAll();
> while($userInfo=array_pop($userInfos)){
> $userInfo = $UserInfos->_fillUserInfoInfo($userInfo);
> $return[$userInfo->getUid()] = $userInfo;
> }
> return $return;
> }

The codes above is just a piece from my current work based on Plog's 
framework.
   
> This is where I get lost :( What would BlogArticles do? What is the
> advantage for us? Why don't we build the caching logic in Articles as
> you've done so far with all the other db-accessor classes?


Quite agree, this is what I thought it is Plog's weekness regarding the 
architecture. In my own work, all my Articles / Blogs classes are
purely db-accessor
classes. I really don't like the idea of doing any logical opertion in the 
's' classes, it will make programme much more complicated.
 The fllowing is a fresh copy from my code generator:
  
> <?php
> /**
> * @author AllanSun
> * @copyright Copyright &copy; 2005, sunajia [AT] gmail [DOT] com
> * @since 2005-05-31 13:08:11
> * @version 0.1
> */
> 
> include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
> include_once( PLOG_CLASS_PATH."class/dao/news.class.php" );
> 
> /**
> * This class provide various operations on the collection of News object.
> */
> 
> class Newss extends Model {
> 
> /**
> * Collection of News objects, indexed by News's Id.
> */
> private $_Newss;
> 
> /**
> * Constructor.
> */
> public function __construct(){
> $this->Model();
> }
> 
> /**
> * Fill the $this->_Newss with the given infomation in
> * array.
> */
> private function _fillNewsInfo($News){
> $return = new News($News);
> return $return;
> }
> 
> public function getById($id){
> $query ="SELECT 
> tblNewss.newsID ,
> tblNewss.newsImageId ,
> tblNewss.datetime ,
> tblNewss.title ,
> tblNewss.body ,
> tblNewss.status 
> FROM
> tblNewss
> WHERE
> newsID='".$id."'";
> $result= $this->Execute($query);
> 
> if( !$result ) return false;
> if ( $result->RecordCount() == 0) return null;
> 
> $row = $result->FetchRow( $result );
> return $this->_fillNewsInfo($row);
> }
> 
> public function getByRange($idx,$nr){
> $pageStart = $nr * ($idx-1) + 1;
> $pageEnd = $nr*$idx;
> $query ="SELECT 
> IDENTITY(int,1,1) AS rownum,
> tblNewss.newsID ,
> tblNewss.newsImageId ,
> tblNewss.datetime ,
> tblNewss.title ,
> tblNewss.body ,
> tblNewss.status 
> INTO
> #TempTable
> FROM
> tblNewss
> ORDER BY
> newsID
> SELECT * FROM #TempTable WHERE rownum BETWEEN ".$pageStart." and 
> ".$pageEnd;
> $result = $this->Execute($query);
> 
> if( !$result ) return false;
> if ( $result->RecordCount() == 0) return null;
> 
> while($row = $result->FetchRow( $result )){
> $News=$this->_fillNewsInfo($row);
> $return[$News->getNewsID()] = $News;
> }
> 
> return $return;
> }
> 
> public function getAll(){
> $query ="SELECT 
> tblNewss.newsID ,
> tblNewss.newsImageId ,
> tblNewss.datetime ,
> tblNewss.title ,
> tblNewss.body ,
> tblNewss.status 
> FROM
> tblNewss";
> $result= $this->Execute($query);
> 
> if( !$result ) return false;
> if ( $result->RecordCount() == 0) return null;
> 
> while($row = $result->FetchRow( $result )){
> $News=$this->_fillNewsInfo($row);
> $return[$News->getNewsID()] = $News;
> }
> return $return;
> }
> 
> public function insert($news){
> $query ="INSERT INTO tblNewss(
> newsImageId ,
> datetime ,
> title ,
> body ,
> status 
> )VALUES('"
> .Db::qstr($news["newsImageId"])."','"
> .Db::qstr($news["datetime"])."','"
> .Db::qstr($news["title"])."','"
> .Db::qstr($news["body"])."','"
> .Db::qstr($news["status"])."'
> )";
> return $this->Execute($query);
> }
> 
> public function delete($id){
> $query ="DELETE FROM 
> tblNewss
> WHERE
> newsID='".$id."'";
> $result = $this->Execute( $query );
> 
> if( !$result ) return false;
> if( $this->_db->Affected_Rows() == 0 ) return false;
> 
> return true;
> }
> 
> public function update($news){
> $query ="UPDATE tblNewss SET 
> newsImageId = '".Db::qstr($news["newsImageId"])."',
> datetime = '".Db::qstr($news["datetime"])."',
> title = '".Db::qstr($news["title"])."',
> body = '".Db::qstr($news["body"])."',
> status = '".Db::qstr($news["status"])."'
> WHERE
> newsID= '".Db::qstr($news["newsID"])."'";
> $result = $this->Execute( $query );
> 
> if( !$result ) return false;
> if( $this->_db->Affected_Rows() == 0 ) return false;
> 
> return true;
> } 
> }
> ?>
> 

My opinion is , DO NOT use these 's' classes as a collection of 
'entity-mapper' clases.

Any body agree with me?

regards,

Allan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.plogworld.net/pipermail/plog-svn/attachments/20050531/d74bb16a/attachment.html


More information about the pLog-svn mailing list