コード例 #1
0
ファイル: MetaData.php プロジェクト: aisuhua/phalcon-php
 /**
  * Initialize the metadata for certain table
  *
  * @param \Phalcon\Mvc\ModelInterface $model
  * @param string|null $key
  * @param string $table
  * @param string $schema
  * @throws Exception
  */
 protected function _initialize(ModelInterface $model, $key = null, $table, $schema)
 {
     if (is_string($table) === false && is_null($table) === false || is_string($schema) === false && is_null($schema) === false || is_string($key) === false && is_null($key) === false) {
         throw new Exception('Invalid parameter type.');
     }
     $strategy = null;
     $className = get_class($model);
     if (is_null($key) === false) {
         //Check for $key in local metadata db
         $metaData = $this->_metaData;
         if (isset($metaData[$key]) === false) {
             //The meta-data is read from the adapter always
             $prefixKey = 'meta-' . $key;
             $data = $this->read($prefixKey);
             if (is_null($data) === false) {
                 //Store the adapters metadata locally
                 if (is_array($metaData) === false) {
                     $metaData = array();
                 }
                 $metaData[$key] = $data;
                 $this->_metaData = $metaData;
             } else {
                 //Check if there is a method 'metaData' in the model to retrieve meta-data form it
                 if (method_exists($model, 'metaData') === true) {
                     $modelMetadata = $model->metaData();
                     if (is_array($modelMetadata) === false) {
                         throw new Exception('Invalid meta-data for model ' . $className);
                     }
                 } else {
                     //Get the meta-data extraction strategy
                     $strategy = $this->getStrategy();
                     //Get the meta-data
                     $modelMetadata = $strategy->getMetaData($model, $this->_dependencyInjector);
                 }
                 //Store the meta-data locally
                 $this->_metaData[$key] = $modelMetadata;
                 //Store the meta-data in the adapter
                 $this->write($prefixKey, $modelMetadata);
             }
         }
     }
     //Check for a column map, store in _columnMap in order and reversed order
     if (isset($GLOBALS['_PHALCON_ORM_COLUMN_RENAMING']) === false || $GLOBALS['_PHALCON_ORM_COLUMN_RENAMING'] === false) {
         return;
     }
     $keyName = strtolower($className);
     if (isset($this->_columnMap[$keyName]) === true) {
         return;
     }
     if (is_array($this->_columnMap) === false) {
         $this->_columnMap = array();
     }
     //Create the map key name
     $prefixKey = 'map-' . $keyName;
     //Check if the meta-data is already in the adapter
     $data = $this->read($prefixKey);
     if (is_null($data) === false) {
         $this->_columnMap[$keyName] = $data;
         return;
     }
     //Get the meta-data extraction strategy
     if (is_object($strategy) === false) {
         $strategy = $this->_dependencyInjector->getStrategy();
     }
     //Get the meta-data
     $modelColumnMap = $strategy->getColumnMaps($model, $this->_dependencyInjector);
     //Update the column map locally
     $this->_columnMap[$keyName] = $modelColumnMap;
     //Write the data to the adapter
     $this->write($prefixKey, $modelColumnMap);
 }