BaseDaoWorker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: BaseDaoWorker.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     abstract class BaseDaoWorker implements BaseDAO
00017     {
00018         const SUFFIX_LIST   = '_list_';
00019         const SUFFIX_INDEX  = '_lists_index_';
00020         const SUFFIX_QUERY  = '_query_';
00021         const SUFFIX_RESULT = '_result_';
00022 
00023         protected $dao = null;
00024         
00025         protected $className = null;
00026         
00027         public function __construct(GenericDAO $dao)
00028         {
00029             $this->dao = $dao;
00030             
00031             $this->className = $dao->getObjectName();
00032         }
00033         
00037         public function setDao(GenericDAO $dao)
00038         {
00039             $this->dao = $dao;
00040             
00041             return $this;
00042         }
00043         
00045 
00046         public function drop(Identifiable $object)
00047         {
00048             return $this->dropById($object->getId());
00049         }
00050         
00051         public function dropById($id)
00052         {
00053             $result =
00054                 DBPool::getByDao($this->dao)->queryNull(
00055                     OSQL::delete()->from($this->dao->getTable())->
00056                     where(Expression::eq($this->dao->getIdName(), $id))
00057                 );
00058             
00059             $this->dao->uncacheById($id);
00060             
00061             return $result;
00062         }
00063         
00064         public function dropByIds(/* array */ $ids)
00065         {
00066             $result =
00067                 DBPool::getByDao($this->dao)->queryNull(
00068                     OSQL::delete()->from($this->dao->getTable())->
00069                     where(Expression::in($this->dao->getIdName(), $ids))
00070                 );
00071             
00072             $this->dao->uncacheByIds($ids);
00073             
00074             return $result;
00075         }
00077 
00079 
00080         public function uncacheById($id)
00081         {
00082             return
00083                 Cache::me()->mark($this->className)->
00084                     delete($this->className.'_'.$id);
00085         }
00086         
00087         public function uncacheByQuery(SelectQuery $query)
00088         {
00089             return
00090                 Cache::me()->mark($this->className)->
00091                     delete($this->className.self::SUFFIX_QUERY.$query->getId());
00092         }
00094         
00096 
00097         public function getCachedById($id)
00098         {
00099             return
00100                 Cache::me()->mark($this->className)->
00101                     get($this->className.'_'.$id);
00102         }
00103         
00104         public function getCachedByQuery(SelectQuery $query)
00105         {
00106             return
00107                 Cache::me()->mark($this->className)->
00108                     get($this->className.self::SUFFIX_QUERY.$query->getId());
00109         }
00111         
00113 
00114         protected function fetchObject(SelectQuery $query)
00115         {
00116             if ($row = DBPool::getByDao($this->dao)->queryRow($query)) {
00117                 return
00118                     $query->getFetchStrategyId() == FetchStrategy::JOIN
00119                         ? $this->dao->makeJoinedObject($row)
00120                         : $this->dao->makeObject($row);
00121             }
00122             
00123             return null;
00124         }
00125         
00126         protected function fetchList(SelectQuery $query)
00127         {
00128             $list = array();
00129             
00130             if ($rows = DBPool::getByDao($this->dao)->querySet($query)) {
00131                 if ($query->getFetchStrategyId() == FetchStrategy::JOIN)
00132                     foreach ($rows as $row)
00133                         $list[] = $this->dao->makeJoinedObject($row);
00134                 else
00135                     foreach ($rows as $row)
00136                         $list[] = $this->dao->makeObject($row);
00137                 
00138                 return $list;
00139             }
00140             
00141             return $list;
00142         }
00144     }
00145 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4