schmunk42\giiant\generators\model\Generator::generate PHP Метод

generate() публичный Метод

public generate ( )
    public function generate()
    {
        $files = [];
        $relations = $this->generateRelations();
        $db = $this->getDbConnection();
        foreach ($this->getTableNames() as $tableName) {
            list($relations, $translations) = array_values($this->extractTranslations($tableName, $relations));
            //var_dump($relations,$tableName);exit;
            $className = php_sapi_name() === 'cli' ? $this->generateClassName($tableName) : $this->modelClass;
            $queryClassName = $this->generateQuery ? $this->generateQueryClassName($className) : false;
            $tableSchema = $db->getTableSchema($tableName);
            $params = ['tableName' => $tableName, 'className' => $className, 'queryClassName' => $queryClassName, 'tableSchema' => $tableSchema, 'labels' => $this->generateLabels($tableSchema), 'hints' => $this->generateHints($tableSchema), 'rules' => $this->generateRules($tableSchema), 'relations' => isset($relations[$tableName]) ? $relations[$tableName] : [], 'ns' => $this->ns, 'enum' => $this->getEnum($tableSchema->columns)];
            if (!empty($translations)) {
                $params['translation'] = $translations;
            }
            $params['blameable'] = $this->generateBlameable($tableSchema);
            $params['timestamp'] = $this->generateTimestamp($tableSchema);
            $files[] = new CodeFile(Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/base/' . $className . $this->baseClassSuffix . '.php', $this->render('model.php', $params));
            $modelClassFile = Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $className . '.php';
            if ($this->generateModelClass || !is_file($modelClassFile)) {
                $files[] = new CodeFile($modelClassFile, $this->render('model-extended.php', $params));
            }
            if ($queryClassName) {
                $queryClassFile = Yii::getAlias('@' . str_replace('\\', '/', $this->queryNs)) . '/' . $queryClassName . '.php';
                if ($this->generateModelClass || !is_file($queryClassFile)) {
                    $params = ['className' => $queryClassName, 'modelClassName' => $className];
                    $files[] = new CodeFile($queryClassFile, $this->render('query.php', $params));
                }
            }
            /*
             * create gii/[name]GiiantModel.json with actual form data
             */
            $suffix = str_replace(' ', '', $this->getName());
            $formDataDir = Yii::getAlias('@' . str_replace('\\', '/', $this->ns));
            $formDataFile = StringHelper::dirname($formDataDir) . '/gii' . '/' . $tableName . $suffix . '.json';
            $formData = json_encode(SaveForm::getFormAttributesValues($this, $this->formAttributes()));
            $files[] = new CodeFile($formDataFile, $formData);
        }
        return $files;
    }

Usage Example

Пример #1
0
 public function testPrefixesGenerator()
 {
     $generator = new ModelGenerator();
     $generator->template = 'default';
     $generator->tableName = 'sakila.*';
     $files = $generator->generate();
     if (version_compare(str_replace('-dev', '', Yii::getVersion()), '2.0.4', '<')) {
         $this->markTestSkipped('This feature is only available since Yii 2.0.4.');
     }
     # TODO: review created files
     #$this->assertEquals(51, count($files));
     #$this->assertEquals("Actor", basename($files[0]->path, '.php'));
     #$this->assertEquals("ActorInfo", basename($files[1]->path, '.php'));
 }