Inheritance: extends CCodeModel
 public function prepare()
 {
     parent::prepare();
     $templatePath = $this->templatePath;
     if (($pos = strrpos($this->tableName, '.')) !== false) {
         $schema = substr($this->tableName, 0, $pos);
         $tableName = substr($this->tableName, $pos + 1);
     } else {
         $schema = '';
         $tableName = $this->tableName;
     }
     if ($tableName[strlen($tableName) - 1] === '*') {
         $tables = Yii::app()->db->schema->getTables($schema);
         if ($this->tablePrefix != '') {
             foreach ($tables as $i => $table) {
                 if (strpos($table->name, $this->tablePrefix) !== 0) {
                     unset($tables[$i]);
                 }
             }
         }
     } else {
         $tables = array($this->getTableSchema($this->tableName));
     }
     $this->relations = $this->generateRelations();
     foreach ($tables as $table) {
         $tableName = $this->removePrefix($table->name);
         $className = $this->generateClassName($table->name);
         $params = array('tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, 'modelClass' => $className, 'columns' => $table->columns, 'labels' => $this->generateLabels($table), 'rules' => $this->generateRules($table), 'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array());
         if ($this->template != 'singlefile') {
             $this->files[] = new CCodeFile(Yii::getPathOfAlias($this->modelPath) . '/' . 'Base' . $className . '.php', $this->render($templatePath . '/basemodel.php', $params));
         }
     }
 }
Ejemplo n.º 2
0
 public function init()
 {
     $parentModule = Yii::app()->controller->getModule('gii')->parentModule;
     if ($parentModule) {
         $moduleId = Yii::app()->controller->getModule('gii')->parentModule->id;
         $moduleFolder = str_replace('snapcms/', '', $moduleId);
         $this->modelPath = "application.modules.snapcms.modules.{$moduleFolder}.models";
     }
     parent::init();
 }
 /**
  * Gets own templates, as well as those from generators.model
  * @return array
  */
 public function getTemplates()
 {
     $templates = parent::getTemplates();
     foreach (Yii::app()->getModule('gii')->generatorPaths as $path) {
         $path = Yii::getPathOfAlias($path . '.model.templates');
         if (is_dir($path)) {
             foreach (scandir($path) as $dir) {
                 if ($dir[0] !== '.' && is_dir($path . '/' . $dir)) {
                     $templates[$dir] = strtr($path . '/' . $dir, array('/' => DIRECTORY_SEPARATOR, '\\' => DIRECTORY_SEPARATOR));
                 }
             }
         }
     }
     return $templates;
 }
Ejemplo n.º 4
0
 public function prepare()
 {
     parent::prepare();
     // Make sure that the CAdvancedArBehavior is in the application components
     // Folder. if it is not, copy it over there
     $path = Yii::getPathOfAlias('application.components');
     if ($path === false) {
         mkdir($path);
     }
     if (!is_dir($path)) {
         die("Fatal Error: Your application components/ is not an directory!");
     }
     $names = scandir($path);
     if (!in_array('CAdvancedArBehavior.php', $names)) {
         $gtcpath = Yii::getPathOfAlias('ext.gtc.vendors');
         if (!copy($gtcpath . '/CAdvancedArBehavior.php', $path . '/CAdvancedArBehavior.php')) {
             die('CAdvancedArBehavior.php could not be copied over to your components directory');
         }
     }
 }
Ejemplo n.º 5
0
 public function prepare()
 {
     parent::prepare();
     $generate_whole_db = false;
     $templatePath = $this->templatePath;
     if (($pos = strrpos($this->tableName, '.')) !== false) {
         $schema = substr($this->tableName, 0, $pos);
         $tableName = substr($this->tableName, $pos + 1);
     } else {
         $schema = '';
         $tableName = $this->tableName;
     }
     if ($tableName[strlen($tableName) - 1] === '*') {
         $generate_whole_db = true;
         $this->tables = Yii::app()->db->schema->getTables($schema);
         if ($this->tablePrefix != '') {
             foreach ($this->tables as $i => $table) {
                 if (strpos($table->name, $this->tablePrefix) !== 0) {
                     unset($this->tables[$i]);
                 }
             }
         }
     } else {
         $this->tables = array($this->getTableSchema($this->tableName));
     }
     $this->relations = $this->generateRelations();
     foreach ($this->tables as $table) {
         $tableName = $this->removePrefix($table->name);
         $className = $this->generateClassName($table->name);
         if (!$this->identificationColumn) {
             $this->identificationColumn = $this->guessIdentificationColumn($table->columns);
         }
         if (!$generate_whole_db && !array_key_exists($this->identificationColumn, $table->columns)) {
             $this->addError('identificationColumn', 'The specified column can not be found in the models attributes. <br /> Please specify a valid attribute. If unsure, leave the field empty.');
         }
         $params = array('tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, 'modelClass' => $className, 'columns' => $table->columns, 'labels' => $this->generateLabels($table), 'rules' => $this->generateRules($table), 'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array());
         if ($this->template != 'singlefile') {
             $this->files[] = new CCodeFile(Yii::getPathOfAlias($this->modelPath) . '/' . 'Base' . $className . '.php', $this->render($templatePath . '/basemodel.php', $params));
         }
     }
 }
 /**
  * @param CCodeFile $file whether the code file should be saved
  *
  * @todo Don't use a constant
  */
 public function confirmed($file)
 {
     if (defined('GIIC_ALL_CONFIRMED') && GIIC_ALL_CONFIRMED === true) {
         return true;
     } else {
         return parent::confirmed($file);
     }
 }
Ejemplo n.º 7
0
 /**
  * Generates the rules for table fields.
  * #MethodTracker
  * This method overrides {@link ModelCode::generateRules}, from version 1.1.7 (r3135). Changes:
  * <ul>
  * <li>Adds the rule to fill empty attributes with null.</li>
  * </ul>
  * @param CDbTableSchema $table The table definition.
  * @return array The rules for the table.
  */
 public function generateRules($table)
 {
     $rules = array();
     $null = array();
     foreach ($table->columns as $column) {
         if ($column->autoIncrement) {
             continue;
         }
         if (!(!$column->allowNull && $column->defaultValue === null)) {
             $null[] = $column->name;
         }
     }
     if ($null !== array()) {
         $rules[] = "array('" . implode(', ', $null) . "', 'default', 'setOnEmpty' => true, 'value' => null)";
     }
     return array_merge(parent::generateRules($table), $rules);
 }
Ejemplo n.º 8
0
 public function attributeLabels()
 {
     return array_merge(parent::attributeLabels(), array('tsBehavior' => 'CTimestampBehavior'));
 }
Ejemplo n.º 9
0
 /**
  * Generate a name for use as a relation name (inside relations() function in a model).
  * @param string the name of the table to hold the relation
  * @param string the foreign key name
  * @param boolean whether the relation would contain multiple objects
  * @return string the relation name
  */
 protected function generateRelationName($tableName, $fkName, $multiple)
 {
     $relationName = parent::generateRelationName($tableName, $fkName, $multiple);
     return preg_replace('/Fk$/', '', $relationName);
 }
Ejemplo n.º 10
0
 public function rules()
 {
     return array_merge(parent::rules(), array(array('relatedRecordBehavior', 'safe')));
 }