コード例 #1
0
ファイル: jDaoParser.class.php プロジェクト: mdouchin/jelix
 protected function import($xml, $tools)
 {
     if (isset($xml['import'])) {
         $import = (string) $xml['import'];
         jApp::pushCurrentModule($this->selector->module);
         // Keep the same driver as current used
         $importSel = new jSelectorDaoDb($import, $this->selector->driver, $this->selector->dbType);
         jApp::popCurrentModule();
         $doc = new DOMDocument();
         if (!$doc->load($importSel->getPath())) {
             throw new jException('jelix~daoxml.file.unknown', $importSel->getPath());
         }
         $parser = new jDaoParser($importSel);
         $parser->parse(simplexml_import_dom($doc), $tools);
         $this->_properties = $parser->getProperties();
         $this->_tables = $parser->getTables();
         $this->_primaryTable = $parser->getPrimaryTable();
         $this->_methods = $parser->getMethods();
         $this->_ojoins = $parser->getOuterJoins();
         $this->_ijoins = $parser->getInnerJoins();
         $this->_eventList = $parser->getEvents();
         $this->_userRecord = $parser->getUserRecord();
         $this->_importedDao = $parser->getImportedDao();
         $this->hasOnlyPrimaryKeys = $parser->hasOnlyPrimaryKeys;
         if ($this->_importedDao) {
             $this->_importedDao[] = $importSel;
         } else {
             $this->_importedDao = array($importSel);
         }
     }
 }
コード例 #2
0
 /**
  * build all classes
  */
 public function buildClasses()
 {
     $src = array();
     $src[] = ' require_once ( JELIX_LIB_PATH .\'dao/jDaoRecordBase.class.php\');';
     $src[] = ' require_once ( JELIX_LIB_PATH .\'dao/jDaoFactoryBase.class.php\');';
     // prepare some values to generate properties and methods
     $this->buildFromWhereClause();
     $this->sqlSelectClause = $this->buildSelectClause();
     $tables = $this->_dataParser->getTables();
     $pkFields = $this->_getPrimaryFieldsList();
     $this->tableRealName = $tables[$this->_dataParser->getPrimaryTable()]['realname'];
     $this->tableRealNameEsc = $this->_encloseName('\'.$this->_conn->prefixTable(\'' . $this->tableRealName . '\').\'');
     $sqlPkCondition = $this->buildSimpleConditions($pkFields);
     if ($sqlPkCondition != '') {
         $sqlPkCondition = ($this->sqlWhereClause != '' ? ' AND ' : ' WHERE ') . $sqlPkCondition;
     }
     //-----------------------
     // Build the record class
     //-----------------------
     $userRecord = $this->_dataParser->getUserRecord();
     if ($userRecord) {
         $src[] = ' require_once (\'' . $userRecord->getPath() . '\');';
         $extendedObject = $userRecord->resource . 'DaoRecord';
     } else {
         $extendedObject = 'jDaoRecordBase';
     }
     $src[] = "\nclass " . $this->_DaoRecordClassName . ' extends ' . $extendedObject . ' {';
     $properties = array();
     foreach ($this->_dataParser->getProperties() as $id => $field) {
         $properties[$id] = get_object_vars($field);
         if ($field->defaultValue !== null) {
             $src[] = ' public $' . $id . '=' . var_export($field->defaultValue, true) . ';';
         } else {
             $src[] = ' public $' . $id . ';';
         }
     }
     $src[] = '   public function getSelector() { return "' . $this->_daoId . '"; }';
     $src[] = '   public function getProperties() { return ' . $this->_DaoClassName . '::$_properties; }';
     $src[] = '   public function getPrimaryKeyNames() { return ' . $this->_DaoClassName . '::$_pkFields; }';
     $src[] = '}';
     //--------------------
     // Build the dao class
     //--------------------
     $src[] = "\nclass " . $this->_DaoClassName . ' extends jDaoFactoryBase {';
     $src[] = '   protected $_tables = ' . var_export($tables, true) . ';';
     $src[] = '   protected $_primaryTable = \'' . $this->_dataParser->getPrimaryTable() . '\';';
     $src[] = '   protected $_selectClause=\'' . $this->sqlSelectClause . '\';';
     $src[] = '   protected $_fromClause;';
     $src[] = '   protected $_whereClause=\'' . $this->sqlWhereClause . '\';';
     $src[] = '   protected $_DaoRecordClassName=\'' . $this->_DaoRecordClassName . '\';';
     $src[] = '   protected $_daoSelector = \'' . $this->_daoId . '\';';
     if ($this->tools->trueValue != '1') {
         $src[] = '   protected $trueValue =' . var_export($this->tools->trueValue, true) . ';';
         $src[] = '   protected $falseValue =' . var_export($this->tools->falseValue, true) . ';';
     }
     if ($this->_dataParser->hasEvent('deletebefore') || $this->_dataParser->hasEvent('delete')) {
         $src[] = '   protected $_deleteBeforeEvent = true;';
     }
     if ($this->_dataParser->hasEvent('deleteafter') || $this->_dataParser->hasEvent('delete')) {
         $src[] = '   protected $_deleteAfterEvent = true;';
     }
     if ($this->_dataParser->hasEvent('deletebybefore') || $this->_dataParser->hasEvent('deleteby')) {
         $src[] = '   protected $_deleteByBeforeEvent = true;';
     }
     if ($this->_dataParser->hasEvent('deletebyafter') || $this->_dataParser->hasEvent('deleteby')) {
         $src[] = '   protected $_deleteByAfterEvent = true;';
     }
     $src[] = '   public static $_properties = ' . var_export($properties, true) . ';';
     $src[] = '   public static $_pkFields = array(' . $this->_writeFieldNamesWith($start = '\'', $end = '\'', $beetween = ',', $pkFields) . ');';
     $src[] = ' ';
     $src[] = 'public function __construct($conn){';
     $src[] = '   parent::__construct($conn);';
     $src[] = '   $this->_fromClause = \'' . $this->sqlFromClause . '\';';
     $src[] = '}';
     $src[] = ' ';
     $src[] = ' protected function _getPkWhereClauseForSelect($pk){';
     $src[] = '   extract($pk);';
     $src[] = ' return \'' . $sqlPkCondition . '\';';
     $src[] = '}';
     $src[] = ' ';
     $src[] = 'protected function _getPkWhereClauseForNonSelect($pk){';
     $src[] = '   extract($pk);';
     $src[] = '   return \' where ' . $this->buildSimpleConditions($pkFields, '', false) . '\';';
     $src[] = '}';
     //----- Insert method
     $src[] = $this->buildInsertMethod($pkFields);
     //-----  update method
     $src[] = $this->buildUpdateMethod($pkFields);
     //----- other user methods
     $src[] = $this->buildUserMethods();
     $src[] = $this->buildEndOfClass();
     $src[] = '}';
     //end of class
     return implode("\n", $src);
 }