Source for file jDaoProperty.class.php

Documentation is available at jDaoProperty.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  dao
  5. @author      Croes GĂ©rald, Laurent Jouanneau
  6. @contributor Laurent Jouanneau
  7. @copyright   2001-2005 CopixTeam, 2005-2011 Laurent Jouanneau
  8. *  This class was get originally from the Copix project (CopixDAODefinitionV1, Copix 2.3dev20050901, http://www.copix.org)
  9. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  10. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  11. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  12. *
  13. @link        http://www.jelix.org
  14. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17. /**
  18.  * Container for properties of a dao property
  19.  * @package  jelix
  20.  * @subpackage dao
  21.  */
  22.  
  23. class jDaoProperty {
  24.     /**
  25.     * the name of the property of the object
  26.     */
  27.     public $name = '';
  28.  
  29.     /**
  30.     * the name of the field in table
  31.     */
  32.     public $fieldName = '';
  33.  
  34.     /**
  35.     * give the regular expression that needs to be matched against.
  36.     * @var string 
  37.     */
  38.     public $regExp = null;
  39.  
  40.     /**
  41.     * says if the field is required when doing a check
  42.     * @var boolean 
  43.     */
  44.     public $required = false;
  45.  
  46.     /**
  47.     * says if the value of the field is required when construct SQL conditions
  48.     * @var boolean 
  49.     */
  50.     public $requiredInConditions = false;
  51.  
  52.     /**
  53.     * Says if it's a primary key.
  54.     * @var boolean 
  55.     */
  56.     public $isPK = false;
  57.  
  58.     /**
  59.     * Says if it's a foreign key
  60.     * @var boolean 
  61.     */
  62.     public $isFK = false;
  63.  
  64.     public $datatype;
  65.  
  66.     public $table=null;
  67.     public $updatePattern='%s';
  68.     public $insertPattern='%s';
  69.     public $selectPattern='%s';
  70.     public $sequenceName='';
  71.  
  72.     /**
  73.     * the maxlength of the key if given
  74.     * @var int 
  75.     */
  76.     public $maxlength = null;
  77.     public $minlength = null;
  78.  
  79.     public $ofPrimaryTable = true;
  80.  
  81.     public $defaultValue = null;
  82.     /**
  83.     * constructor.
  84.     */
  85.     function __construct ($aParams$def){
  86.         $needed array('name''fieldname''table''datatype''required',
  87.                         'minlength''maxlength''regexp''sequence''default');
  88.  
  89.         $params $def->getAttr($aParams$needed);
  90.  
  91.         if ($params['name']===null){
  92.             throw new jDaoXmlException ('missing.attr'array('name''property'));
  93.         }
  94.         $this->name       = $params['name'];
  95.  
  96.         if(!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'$this->name)){
  97.             throw new jDaoXmlException ('property.invalid.name'$this->name);
  98.         }
  99.  
  100.  
  101.         $this->fieldName  = $params['fieldname'!==null $params['fieldname'$this->name;
  102.         $this->table      = $params['table'!==null $params['table'$def->getPrimaryTable();
  103.  
  104.         $tables $def->getTables();
  105.  
  106.         if(!isset$tables[$this->table])){
  107.             throw new jDaoXmlException ('property.unknow.table'$this->name);
  108.         }
  109.  
  110.         $this->required   = $this->requiredInConditions = $def->getBool ($params['required']);
  111.         $this->maxlength  = $params['maxlength'!== null intval($params['maxlength']null;
  112.         $this->minlength  = $params['minlength'!== null intval($params['minlength']null;
  113.         $this->regExp     = $params['regexp'];
  114.  
  115.         if ($params['datatype']===null){
  116.             throw new jDaoXmlException ('missing.attr'array('datatype''property'));
  117.         }
  118.         $params['datatype']=trim(strtolower($params['datatype']));
  119.  
  120.         if (!in_array ($params['datatype'],
  121.                        array ('autoincrement''bigautoincrement''int',
  122.                               'datetime''time''integer''varchar''string',
  123.                               'text''varchardate''date''numeric''double',
  124.                               'float''boolean''varbinary'))){
  125.            throw new jDaoXmlException ('wrong.attr'array($params['datatype'],
  126.                                                            $this->fieldName,
  127.                                                            'property'));
  128.         }
  129.         $this->datatype = strtolower($params['datatype']);
  130.         $this->needsQuotes in_array ($params['datatype'],
  131.                 array ('string''varchar''text''date''datetime''time''varbinary'));
  132.  
  133.         $this->isPK = in_array($this->fieldName$tables[$this->table]['pk']);
  134.         if(!$this->isPK && $this->table == $def->getPrimaryTable()){
  135.             foreach($tables as $table=>$info{
  136.                 if ($table == $this->table)
  137.                     continue;
  138.                 if (isset($info['fk']&& in_array($this->fieldName$info['fk'])) {
  139.                     $this->isFK = true;
  140.                     break;
  141.                 }
  142.             }
  143.         }
  144.         else {
  145.             $this->required = true;
  146.             $this->requiredInConditions = true;
  147.         }
  148.  
  149.         if($this->datatype == 'autoincrement' || $this->datatype == 'bigautoincrement'{
  150.             if($params['sequence'!==null){
  151.                 $this->sequenceName = $params['sequence'];
  152.             }
  153.             $this->required = false;
  154.             $this->requiredInConditions = true;
  155.         }
  156.  
  157.         if($params['default'!== null{
  158.             switch($this->datatype{
  159.               case 'autoincrement':
  160.               case 'int':
  161.               case 'integer':
  162.                 $this->defaultValue = intval($params['default']);
  163.                 break;
  164.               case 'double':
  165.               case 'float':
  166.                 $this->defaultValue = doubleval($params['default']);
  167.                 break;
  168.               case 'boolean':
  169.                 $v $params['default'];
  170.                 $this->defaultValue = (intval($v== 1|| $v==='t'|| strtolower($v=='true');
  171.                 break;
  172.               default:
  173.                 $this->defaultValue = $params['default'];
  174.             }
  175.         }
  176.  
  177.         // on ignore les attributs *pattern sur les champs PK et FK
  178.         if(!$this->isPK && !$this->isFK){
  179.             if(isset($aParams['updatepattern'])) {
  180.                 $this->updatePattern=(string)$aParams['updatepattern'];
  181.             }
  182.  
  183.             if(isset($aParams['insertpattern'])) {
  184.                 $this->insertPattern=(string)$aParams['insertpattern'];
  185.             }
  186.  
  187.             if(isset($aParams['selectpattern'])) {
  188.                 $this->selectPattern=(string)$aParams['selectpattern'];
  189.             }
  190.         }
  191.  
  192.         // no update and insert patterns for field of external tables
  193.         if($this->table != $def->getPrimaryTable()){
  194.             $this->updatePattern = '';
  195.             $this->insertPattern = '';
  196.             $this->required = false;
  197.             $this->requiredInConditions = false;
  198.             $this->ofPrimaryTable = false;
  199.         }else{
  200.             $this->ofPrimaryTable=true;
  201.         }
  202.     }
  203. }

Documentation generated on Thu, 22 Mar 2012 22:14:59 +0100 by phpDocumentor 1.4.3