ModelCode::prepare PHP Method

prepare() public method

public prepare ( )
    public function prepare()
    {
        $this->files = array();
        $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()->{$this->connectionId}->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(), 'connectionId' => $this->connectionId);
            $this->files[] = new CCodeFile(Yii::getPathOfAlias($this->modelPath) . '/' . $className . '.php', $this->render($templatePath . '/model.php', $params));
        }
    }

Usage Example

 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()->{$this->connectionId}->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);
         $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));
         }
     }
 }
All Usage Examples Of ModelCode::prepare