Ticket #5: daoRecordExtended.diff

File daoRecordExtended.diff, 7.3 kB (added by sylvain261, 7 months ago)

Possibilité d'étendre les objets records

  • build/manifests/jelix-lib.mn

    old new  
    270270  jDao.class.php 
    271271* jDaoCompiler.class.php 
    272272  jDaoConditions.class.php 
     273  jDaoExtended.class.php 
     274  jDaoRecordBaseExtended.class.php 
    273275 
    274276cd lib/jelix/db 
    275277* jDb.class.php 
  • lib/jelix/dao/jDaoExtended.class.php

    old new  
     1<?php 
     2/** 
     3* @package    jelix 
     4* @subpackage dao 
     5* @author     Sylvain de vathaire 
     6* @contributor 
     7* @copyright   2008 Neov 
     8* @link        http://www.jelix.org 
     9* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     10*/ 
     11 
     12/** 
     13 * 
     14 */ 
     15require_once(JELIX_LIB_PATH.'dao/jDao.class.php'); 
     16 
     17/** 
     18 * Factory to create DAO objects with supports of the extended records 
     19 * @package  jelix 
     20 * @subpackage dao 
     21 */ 
     22class jDaoExtended extends jDao { 
     23 
     24    /** 
     25    * creates a new instance of a DAO. 
     26    * If no dao is founded, try to compile a DAO from the dao xml file 
     27    * @param string|jSelectorDao $Daoid the dao selector 
     28    * @param string $profil the db profil name to use for the connection.  
     29    *   If empty, use the default profil 
     30    * @return jDaoFactoryBase  the dao object 
     31    */ 
     32    public static function create ($DaoId, $recordClassName = '', $profil=''){ 
     33 
     34        if(!is_object($DaoId)) 
     35            $sel = new jSelectorDao($DaoId, $profil); 
     36        else $sel = $DaoId; 
     37 
     38 
     39        $c = $sel->getDaoClass(); 
     40        if(!class_exists($c,false)){ 
     41            jIncluder::inc($sel); 
     42        } 
     43        $conn = jDb::getConnection ($profil); 
     44        if($recordClassName!= ''){ 
     45            jClasses::inc($sel->module.'~'.$recordClassName); 
     46            $obj = new $c ($conn, $recordClassName); 
     47        }else{ 
     48            $obj = new $c ($conn); 
     49        } 
     50        return $obj; 
     51    } 
     52 
     53    /** 
     54    * return a DAO instance. It Handles a singleton of the DAO. 
     55    * If no dao is founded, try to compile a DAO from the dao xml file 
     56    * @param string|jSelectorDao $Daoid the dao selector 
     57    * @param string $profil the db profil name to use for the connection.  
     58    *   If empty, use the default profil 
     59    * @return jDaoFactoryBase  the dao object 
     60    */ 
     61    public static function get ($DaoId, $recordClassName='', $profil='') { 
     62       static $_daoSingleton=array(); 
     63 
     64       $sel = new jSelectorDao($DaoId, $profil); 
     65       $DaoId = $sel->toString (); 
     66 
     67        if (! isset ($_daoSingleton[$DaoId.$recordClassName])){ 
     68            $_daoSingleton[$DaoId.$recordClassName] = self::create ($sel, $recordClassName, $profil); 
     69        } 
     70        return $_daoSingleton[$DaoId.$recordClassName]; 
     71    } 
     72} 
     73?> 
  • lib/jelix/dao/jDaoGenerator.class.php

    old new  
    136136        $src[] = '   public static $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');'; 
    137137 
    138138        $src[] = ' '; 
    139         $src[] = 'public function __construct($conn){'; 
     139        $src[] = 'public function __construct($conn, $DaoRecordClassName = \'\'){'; 
    140140        $src[] = '   parent::__construct($conn);'; 
    141141        $src[] = '   $this->_fromClause = \''.$sqlFromClause.'\';'; 
     142        $src[] = '      if($DaoRecordClassName != \'\'){'; 
     143        $src[] = '          $this->_DaoRecordClassName = $DaoRecordClassName;'; 
     144        $src[] = '      }'; 
    142145        $src[] = '}'; 
    143146 
    144147        // cannot put this methods directly into jDaoBase because of a php bug on static methods/properties 
  • lib/jelix/dao/jDaoRecordBaseExtended.class.php

    old new  
     1<?php 
     2/** 
     3 * @package     jelix 
     4 * @subpackage  dao 
     5 * @author      Sylvain de Vathaire 
     6 * @contributor Loic Mathaud 
     7 * @copyright   2008 Neov 
     8 * @copyright   2007 Loic Mathaud 
     9 * @link        http://www.jelix.org 
     10 * @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     11 */ 
     12 
     13 
     14/** 
     15 * 
     16 */ 
     17require_once(JELIX_LIB_PATH.'dao/jDaoRecordBase.class.php'); 
     18 
     19 
     20/** 
     21 * Base class for all extended record classes 
     22 * @package  jelix 
     23 * @subpackage dao 
     24 * @see jDaoExtended 
     25 */ 
     26class jDaoRecordBaseExtended extends jDaoRecordBase{ 
     27 
     28    private $_properties; 
     29 
     30    function __construct(){ 
     31       $propertiesName = array_keys($this->getProperties()); 
     32       foreach($propertiesName as $propertyName){ 
     33           $this->_properties[$propertyName] = null; 
     34       } 
     35    } 
     36 
     37    final private function __get($property) { 
     38        if (array_key_exists($property, $this->_properties)) { 
     39            return $this->_properties[$property]; 
     40        }else{ 
     41            return null; 
     42        } 
     43      } 
     44 
     45    final private function __set($property, $val){ 
     46        if (array_key_exists($property, $this->_properties)){ 
     47            $this->_properties[$property] = $val; 
     48        }else{ 
     49            //TODO : A revoir comment faire propre 
     50            throw new Exception('Invalid property '.$property.' for class '.get_class($this)); 
     51        } 
     52    } 
     53 
     54    private function __isset($property){ 
     55        return array_key_exists($property, $this->_properties); 
     56    } 
     57 
     58    private function __unset($property){ 
     59        unset($this->_properties[$property]); 
     60    } 
     61 
     62     
     63    /** 
     64     * @return array informations on all properties 
     65     * @see jDaoFactoryBase::getProperties() 
     66     */ 
     67    public function getProperties(){ 
     68        $vars = get_class_vars($this->_getFactoryClassName()); 
     69        return $vars['_properties']; 
     70    } 
     71 
     72    /** 
     73     * @return array list of properties name which contains primary keys 
     74     * @see jDaoFactoryBase::getPrimaryKeyNames() 
     75     * @since 1.0b3 
     76     */ 
     77     public function getPrimaryKeyNames(){ 
     78        $vars = get_class_vars($this->_getFactoryClassName()); 
     79        return $vars['_pkFields']; 
     80     } 
     81 
     82    /** 
     83     * @return string class name of the factory used to instanciate the record 
     84     */ 
     85    private function _getFactoryClassName(){ 
     86 
     87        static $factoryClassName = ''; 
     88        if($factoryClassName != ''){ 
     89            return $factoryClassName; 
     90        } 
     91        //Hack : In order to get a dao selector, we look for the module name in the class file path 
     92        $className = get_class($this); 
     93        $class = new ReflectionClass($className); 
     94        $classPath = $class->getFileName(); 
     95        $classPath = substr($classPath, strrpos($classPath, DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR));  
     96        $moduleName = substr($classPath, 9, strrpos($classPath, DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR)-9);  
     97 
     98        $daoId = substr($className, 0, strrpos($className, 'Record')); 
     99        $daoId = $moduleName.'~'.$daoId; 
     100 
     101        $sel = new jSelectorDao($daoId, ''); 
     102        $factoryClassName = $sel->getDaoClass(); 
     103 
     104        return $factoryClassName; 
     105    } 
     106 
     107} 
     108 
     109?> 
Download in other formats: Original Format