Changeset 483

Show
Ignore:
Timestamp:
07/01/07 17:16:21 (2 years ago)
Author:
laurentj
Message:

- Fixed a notice in jDaoRecordBase::setPk(), and added jDaoRecordBase::getPk()
- jForms::saveToDao returns now the value of primaryKeys
- added new unit tests on jForms and jFormsBase classes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/testapp.mn

    r473 r483  
    9090cd testapp/modules/jelix_tests/daos 
    9191  products.dao.xml 
     92  description.dao.xml 
     93cd testapp/modules/jelix_tests/forms 
     94  product.form.xml 
    9295 
    9396cd testapp/modules/jelix_tests/tests/ 
     
    109112  jdb.1_queries.html.php 
    110113  jdb.2_queries_with_pdo.html.php 
     114  jforms.html.php 
    111115  jforms.compiler.html.php 
     116  jforms.with_dao.html.php 
    112117  jtpl.expressions_parsing.html_cli.php 
    113118  utils.jfilter.html_cli.php 
    114  
    115119 
    116120cd testapp/var 
  • trunk/build/manifests/testapp.mn

    r473 r483  
    9090cd testapp/modules/jelix_tests/daos 
    9191  products.dao.xml 
     92  description.dao.xml 
     93cd testapp/modules/jelix_tests/forms 
     94  product.form.xml 
    9295 
    9396cd testapp/modules/jelix_tests/tests/ 
     
    109112  jdb.1_queries.html.php 
    110113  jdb.2_queries_with_pdo.html.php 
     114  jforms.html.php 
    111115  jforms.compiler.html.php 
     116  jforms.with_dao.html.php 
    112117  jtpl.expressions_parsing.html_cli.php 
    113118  utils.jfilter.html_cli.php 
    114  
    115119 
    116120cd testapp/var 
  • trunk/lib/jelix-modules/junittests/classes/junittestcase.class.php

    r468 r483  
    157157 
    158158                if(!$ok) 
    159                     $this->fail($name.' : objets non identiques'.$errormessage); 
     159                    $this->fail($name.' : non identical objects'.$errormessage); 
    160160                return $ok; 
    161161 
     
    180180                            $n = $key ++; 
    181181                        } 
    182                         if($this->assertTrue(isset($value[$n]),$name.'['.$n.'] doesn\'t exists'.$errormessage)){ 
     182/*$this->dump($n, 'n='); 
     183$this->dump($value, 'value'); 
     184if(isset($value[$n])) 
     185$this->dump($value[$n],'value de n OK'); 
     186else 
     187$this->dump('!!!!! value de n  pas ok');*/ 
     188                        if($this->assertTrue(array_key_exists($n,$value),$name.'['.$n.'] doesn\'t exist arrrg'.$errormessage)){ 
    183189                            $v = $value[$n]; 
    184190                            $ok &= $this->_checkIdentical($child, $v, $name.'['.$n.']',$errormessage); 
  • trunk/lib/jelix-modules/junittests/classes/junittestcase.class.php

    r468 r483  
    157157 
    158158                if(!$ok) 
    159                     $this->fail($name.' : objets non identiques'.$errormessage); 
     159                    $this->fail($name.' : non identical objects'.$errormessage); 
    160160                return $ok; 
    161161 
     
    180180                            $n = $key ++; 
    181181                        } 
    182                         if($this->assertTrue(isset($value[$n]),$name.'['.$n.'] doesn\'t exists'.$errormessage)){ 
     182/*$this->dump($n, 'n='); 
     183$this->dump($value, 'value'); 
     184if(isset($value[$n])) 
     185$this->dump($value[$n],'value de n OK'); 
     186else 
     187$this->dump('!!!!! value de n  pas ok');*/ 
     188                        if($this->assertTrue(array_key_exists($n,$value),$name.'['.$n.'] doesn\'t exist arrrg'.$errormessage)){ 
    183189                            $v = $value[$n]; 
    184190                            $ok &= $this->_checkIdentical($child, $v, $name.'['.$n.']',$errormessage); 
  • trunk/lib/jelix/dao/jDaoBase.class.php

    r471 r483  
    4848     * @var array  
    4949     */ 
    50     protected $_properties=array(); 
     50    protected $_properties = array(); 
     51 
     52    /** 
     53     * list of id of primary key properties 
     54     * @var array 
     55     */ 
     56    protected $_pkFields = array(); 
    5157 
    5258    /** 
     
    116122    public function setPk(){ 
    117123        $args=func_get_args(); 
    118         if(count($args) == 0) throw new jException('jelix~dao.error.keys.missing'); 
    119124        if(count($args)==1 && is_array($args[0])){ 
    120125            $args=$args[0]; 
    121126        } 
    122         $i=0; 
    123         foreach($this->_properties as $prop=>$infos){ 
    124             if($infos['isPk']){ 
    125                 if($i>= count($args)) 
    126                     throw new jException('jelix~dao.error.keys.missing'); 
    127                 $this->$prop = $args[$i++]; 
    128             } 
     127        if(count($args) == 0 || count($args) != count($this->_pkFields) )  
     128            throw new jException('jelix~dao.error.keys.missing'); 
     129 
     130        foreach($this->_pkFields as $k=>$prop){ 
     131           $this->$prop = $args[$k]; 
    129132        } 
    130133        return true; 
     134    } 
     135     
     136    /** 
     137     * return the value of fields corresponding to the primary key 
     138     * @return mixed  the value or an array of values if there is several  pk 
     139     * @since 1.0beta3 
     140     */ 
     141    public function getPk(){ 
     142        if(count($this->_pkFields) == 1){ 
     143            return $this->{$this->_pkFields[0]}; 
     144        }else{ 
     145            $list = array(); 
     146            foreach($this->_pkFields as $k=>$prop){ 
     147                $list[] = $this->$prop; 
     148            } 
     149            return $list; 
     150        } 
    131151    } 
    132152} 
  • trunk/lib/jelix/dao/jDaoBase.class.php

    r471 r483  
    4848     * @var array  
    4949     */ 
    50     protected $_properties=array(); 
     50    protected $_properties = array(); 
     51 
     52    /** 
     53     * list of id of primary key properties 
     54     * @var array 
     55     */ 
     56    protected $_pkFields = array(); 
    5157 
    5258    /** 
     
    116122    public function setPk(){ 
    117123        $args=func_get_args(); 
    118         if(count($args) == 0) throw new jException('jelix~dao.error.keys.missing'); 
    119124        if(count($args)==1 && is_array($args[0])){ 
    120125            $args=$args[0]; 
    121126        } 
    122         $i=0; 
    123         foreach($this->_properties as $prop=>$infos){ 
    124             if($infos['isPk']){ 
    125                 if($i>= count($args)) 
    126                     throw new jException('jelix~dao.error.keys.missing'); 
    127                 $this->$prop = $args[$i++]; 
    128             } 
     127        if(count($args) == 0 || count($args) != count($this->_pkFields) )  
     128            throw new jException('jelix~dao.error.keys.missing'); 
     129 
     130        foreach($this->_pkFields as $k=>$prop){ 
     131           $this->$prop = $args[$k]; 
    129132        } 
    130133        return true; 
     134    } 
     135     
     136    /** 
     137     * return the value of fields corresponding to the primary key 
     138     * @return mixed  the value or an array of values if there is several  pk 
     139     * @since 1.0beta3 
     140     */ 
     141    public function getPk(){ 
     142        if(count($this->_pkFields) == 1){ 
     143            return $this->{$this->_pkFields[0]}; 
     144        }else{ 
     145            $list = array(); 
     146            foreach($this->_pkFields as $k=>$prop){ 
     147                $list[] = $this->$prop; 
     148            } 
     149            return $list; 
     150        } 
    131151    } 
    132152} 
  • trunk/lib/jelix/dao/jDaoGenerator.class.php

    r450 r483  
    6262      $src[] = ' require_once ( JELIX_LIB_DAO_PATH .\'jDaoBase.class.php\');'; 
    6363 
    64       //----------------------- 
    65       // Build the record class 
    66       //----------------------- 
    67  
    68       $src[] = "\nclass ".$this->_DaoRecordClassName.' extends jDaoRecordBase {'; 
    69  
    70       $properties=array(); 
    71  
    72       foreach ($this->_datasParser->getProperties() as $id=>$field){ 
    73           $properties[$id] = get_object_vars($field); 
    74           $src[] =' public $'.$id.';'; 
    75       } 
    76  
    77       $src[] = ' protected $_properties = '.var_export($properties, true).';'; 
    78  
    79       $src[] = '}'; 
    80  
    81       //-------------------- 
    82       // Build the dao class 
    83       //-------------------- 
    84  
    85       // prepare some values to generate methods 
     64      // prepare some values to generate properties and methods 
    8665 
    8766      list($sqlFromClause, $sqlWhereClause)= $this->_getFromClause(); 
     
    9776      } 
    9877 
     78      //----------------------- 
     79      // Build the record class 
     80      //----------------------- 
     81 
     82      $src[] = "\nclass ".$this->_DaoRecordClassName.' extends jDaoRecordBase {'; 
     83 
     84      $properties=array(); 
     85 
     86      foreach ($this->_datasParser->getProperties() as $id=>$field){ 
     87          $properties[$id] = get_object_vars($field); 
     88          $src[] =' public $'.$id.';'; 
     89      } 
     90 
     91      $src[] = ' protected $_properties = '.var_export($properties, true).';'; 
     92      $src[] = ' protected $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');'; 
     93 
     94      $src[] = '}'; 
     95 
     96      //-------------------- 
     97      // Build the dao class 
     98      //-------------------- 
     99 
     100 
    99101      $src[] = "\nclass ".$this->_DaoClassName.' extends jDaoFactoryBase {'; 
    100102      $src[] ='   protected $_tables = '.var_export($tables, true).';'; 
     
    105107      $src[] ='   protected $_DaoRecordClassName=\''.$this->_DaoRecordClassName.'\';'; 
    106108      $src[] ='   protected $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');'; 
    107  
    108109 
    109110      $src[] = ' '; 
  • trunk/lib/jelix/dao/jDaoGenerator.class.php

    r450 r483  
    6262      $src[] = ' require_once ( JELIX_LIB_DAO_PATH .\'jDaoBase.class.php\');'; 
    6363 
    64       //----------------------- 
    65       // Build the record class 
    66       //----------------------- 
    67  
    68       $src[] = "\nclass ".$this->_DaoRecordClassName.' extends jDaoRecordBase {'; 
    69  
    70       $properties=array(); 
    71  
    72       foreach ($this->_datasParser->getProperties() as $id=>$field){ 
    73           $properties[$id] = get_object_vars($field); 
    74           $src[] =' public $'.$id.';'; 
    75       } 
    76  
    77       $src[] = ' protected $_properties = '.var_export($properties, true).';'; 
    78  
    79       $src[] = '}'; 
    80  
    81       //-------------------- 
    82       // Build the dao class 
    83       //-------------------- 
    84  
    85       // prepare some values to generate methods 
     64      // prepare some values to generate properties and methods 
    8665 
    8766      list($sqlFromClause, $sqlWhereClause)= $this->_getFromClause(); 
     
    9776      } 
    9877 
     78      //----------------------- 
     79      // Build the record class 
     80      //----------------------- 
     81 
     82      $src[] = "\nclass ".$this->_DaoRecordClassName.' extends jDaoRecordBase {'; 
     83 
     84      $properties=array(); 
     85 
     86      foreach ($this->_datasParser->getProperties() as $id=>$field){ 
     87          $properties[$id] = get_object_vars($field); 
     88          $src[] =' public $'.$id.';'; 
     89      } 
     90 
     91      $src[] = ' protected $_properties = '.var_export($properties, true).';'; 
     92      $src[] = ' protected $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');'; 
     93 
     94      $src[] = '}'; 
     95 
     96      //-------------------- 
     97      // Build the dao class 
     98      //-------------------- 
     99 
     100 
    99101      $src[] = "\nclass ".$this->_DaoClassName.' extends jDaoFactoryBase {'; 
    100102      $src[] ='   protected $_tables = '.var_export($tables, true).';'; 
     
    105107      $src[] ='   protected $_DaoRecordClassName=\''.$this->_DaoRecordClassName.'\';'; 
    106108      $src[] ='   protected $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');'; 
    107  
    108109 
    109110      $src[] = ' '; 
  • trunk/lib/jelix/forms/jForms.class.php

    r473 r483  
    9191     */ 
    9292    static public function fill($formSel,$formId=JFORMS_DEFAULT_ID){ 
    93         if($formId === null) $formId=JFORMS_DEFAULT_ID; 
    9493        $form = self::get($formSel,$formId); 
    9594        if($form) 
     
    113112      } 
    114113   } 
    115  
    116114} 
    117115 
  • trunk/lib/jelix/forms/jForms.class.php

    r473 r483  
    9191     */ 
    9292    static public function fill($formSel,$formId=JFORMS_DEFAULT_ID){ 
    93         if($formId === null) $formId=JFORMS_DEFAULT_ID; 
    9493        $form = self::get($formSel,$formId); 
    9594        if($form) 
     
    113112      } 
    114113   } 
    115  
    116114} 
    117115 
  • trunk/lib/jelix/forms/jFormsBase.class.php

    r473 r483  
    7171            //if($value !== null) on commente pour le moment, 
    7272            //@todo à prevoir un meilleur test, pour les formulaires sur plusieurs pages 
     73            if($value === null) $value=''; 
    7374            $this->_container->datas[$name]= $value; 
    7475        } 
     
    118119        } 
    119120    } 
     121 
    120122    /** 
    121123     * save datas using a dao. 
    122124     * it call insert or update depending the value of the formId stored in the container 
    123125     * @param string $daoSelector the selector of a dao file 
     126     * @return mixed  the primary key of the new record in a case of inserting 
    124127     * @see jDao 
    125128     */ 
     
    136139            $dao->insert($daorec); 
    137140        } 
     141        return $daorec->getPk(); 
    138142    } 
    139143 
  • trunk/lib/jelix/forms/jFormsBase.class.php

    r473 r483  
    7171            //if($value !== null) on commente pour le moment, 
    7272            //@todo à prevoir un meilleur test, pour les formulaires sur plusieurs pages 
     73            if($value === null) $value=''; 
    7374            $this->_container->datas[$name]= $value; 
    7475        } 
     
    118119        } 
    119120    } 
     121 
    120122    /** 
    121123     * save datas using a dao. 
    122124     * it call insert or update depending the value of the formId stored in the container 
    123125     * @param string $daoSelector the selector of a dao file 
     126     * @return mixed  the primary key of the new record in a case of inserting 
    124127     * @see jDao 
    125128     */ 
     
    136139            $dao->insert($daorec); 
    137140        } 
     141        return $daorec->getPk(); 
    138142    } 
    139143 
  • trunk/testapp/modules/jelix_tests/tests/jdao.main_api.html.php

    r468 r483  
    3232 
    3333    function testRecordCheck() { 
    34 //TODO 
     34 
     35        $record = jDao::createRecord ('products'); 
     36        $this->assertEqual($record->id , ''); 
     37        $record->setPk(5); 
     38        $this->assertEqual($record->id , 5); 
     39 
     40        $this->assertEqual($record->getPk(), 5); 
     41  
     42        $record = jDao::createRecord ('description'); 
     43        $this->assertEqual($record->id , ''); 
     44        $this->assertEqual($record->lang , ''); 
     45 
     46        $record->setPk(5,'fr'); 
     47        $this->assertEqual($record->id , 5); 
     48        $this->assertEqual($record->lang , 'fr'); 
     49 
     50        $record->setPk(array(4,'en')); 
     51        $this->assertEqual($record->id , 4); 
     52        $this->assertEqual($record->lang , 'en'); 
     53 
     54        $pk = $record->getPk(); 
     55        $this->assertEqual($pk, array(4,'en')); 
    3556    } 
    3657 
  • trunk/testapp/modules/jelix_tests/tests/jdao.main_api.html.php

    r468 r483  
    3232 
    3333    function testRecordCheck() { 
    34 //TODO 
     34 
     35        $record = jDao::createRecord ('products'); 
     36        $this->assertEqual($record->id , ''); 
     37        $record->setPk(5); 
     38        $this->assertEqual($record->id , 5); 
     39 
     40        $this->assertEqual($record->getPk(), 5); 
     41  
     42        $record = jDao::createRecord ('description'); 
     43        $this->assertEqual($record->id , ''); 
     44        $this->assertEqual($record->lang , ''); 
     45 
     46        $record->setPk(5,'fr'); 
     47        $this->assertEqual($record->id , 5); 
     48        $this->assertEqual($record->lang , 'fr'); 
     49 
     50        $record->setPk(array(4,'en')); 
     51        $this->assertEqual($record->id , 4); 
     52        $this->assertEqual($record->lang , 'en'); 
     53 
     54        $pk = $record->getPk(); 
     55        $this->assertEqual($pk, array(4,'en')); 
    3556    } 
    3657 
  • trunk/testapp/modules/jelix_tests/tests/jforms.compiler.html.php

    r473 r483  
    2626 
    2727 
    28 class UTjforms extends jUnitTestCase { 
     28class UTjformsCompiler extends jUnitTestCase { 
    2929 
    3030    protected $_XmlControls = array( 
  • trunk/testapp/modules/jelix_tests/tests/jforms.compiler.html.php

    r473 r483  
    2626 
    2727 
    28 class UTjforms extends jUnitTestCase { 
     28class UTjformsCompiler extends jUnitTestCase { 
    2929 
    3030    protected $_XmlControls = array( 
Download in other formats: Unified Diff Zip Archive