예제 #1
0
 public function generateSettersAndGetters()
 {
     foreach ($this->fields as $field) {
         $fieldName = $field->getName();
         $setFieldMethod = new AbstractClassMethod($field->getSetterName());
         $param = new AbstractMethodParam(SbUtils::wordUnderscoreToCamelCaseFirstLower($field->getName()));
         $setFieldMethod->addParam($param);
         $setFieldMethod->addContentLine('$this->' . $fieldName . ' = $' . SbUtils::wordUnderscoreToCamelCaseFirstLower($fieldName) . ';');
         $this->addMethod($setFieldMethod);
         $getFieldMethod = new AbstractClassMethod($field->getGetterName());
         $getFieldMethod->addContentLine("return \$this->{$fieldName};");
         $this->addMethod($getFieldMethod);
     }
 }
 public function getGetterName()
 {
     return "get" . SbUtils::wordUnderscoreToCamelCase($this->name);
 }
예제 #3
0
 public function generate($options = array())
 {
     $dbScheme = new Scheme($this->di);
     $tables = $dbScheme->getScheme($options);
     $this->relations = [];
     //var_dump($tables);
     if (!is_dir($this->entityDir . "\\" . 'Generated')) {
         mkdir($this->entityDir . "\\" . 'Generated', 0777, true);
     }
     $this->generateCommonMethods();
     foreach ($tables as $table) {
         $tableClass = new AbstractClass($table['model']);
         $tableClass->setExtends('Basic');
         $tableClass->setNamespace($this->entityNamespaceGenerated);
         $tableClassChild = new AbstractClass($table['model']);
         $tableClassChild->setExtends("Generated\\" . $table['model']);
         $tableClassChild->setNamespace($this->entityNamespace);
         foreach ($table['columns'] as $field) {
             $fieldField = new AbstractClassField($field);
             $fieldField->setScope('protected');
             $tableClass->addField($fieldField);
         }
         $getSourceMethod = new AbstractClassMethod("getSource");
         $getSourceMethod->setScope("public");
         $getSourceMethod->addContentLine("return '{$table['name']}';");
         $tableClass->addMethod($getSourceMethod);
         /*$onConstructMethod = new AbstractClassMethod("onConstruct");
                     $tableClass->addMethod($onConstructMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeSave");
                     $beforeSaveMethod->addContentLine("parent::beforeSave()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeCreate");
                     $beforeSaveMethod->addContentLine("parent::beforeCreate()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $beforeSaveMethod = new AbstractClassMethod("beforeUpdate");
                     $beforeSaveMethod->addContentLine("parent::beforeUpdate()");
                     $tableClass->addMethod($beforeSaveMethod);
         
                     $afterFetchMethod = new AbstractClassMethod("afterFetch");
                     $beforeSaveMethod->addContentLine("parent::afterFetch()");
                     $tableClass->addMethod($afterFetchMethod);*/
         $initializeMethod = new AbstractClassMethod("initialize");
         $initializeMethod->addContentLine('$this->useDynamicUpdate(true);');
         if (isset($table['ref_many_to_one'])) {
             $table['ref_many_to_one'] = $this->prepareRef($table['ref_many_to_one']);
             foreach ($table['ref_many_to_one'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $initializeMethod->addContentLine("\$this->belongsTo('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . $aliasModel);
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}");
                 $getMethodParam1 = new AbstractMethodParam("parameters");
                 $getMethodParam1->setDefaultValue("null");
                 $getMethod->addParam($getMethodParam1);
                 $variableName = lcfirst($aliasModel);
                 $setMethod = new AbstractClassMethod('set' . $aliasModel);
                 $setMethodParam1 = new AbstractMethodParam($variableName);
                 $setMethodParam1->setDocType("\\{$this->entityNamespace}\\{$ref['model']}|null");
                 $setMethod->addParam($setMethodParam1);
                 $setMethod->addContentLine("\$this->{$ref['column']} = \${$variableName} ? \${$variableName}->getId() : null;");
                 $setMethod->addContentLine("return \$this;");
                 $setMethod->setReturn('$this');
                 $tableClass->addMethod($getMethod);
                 $tableClass->addMethod($setMethod);
             }
         }
         if (isset($table['ref_one_to_many'])) {
             $table['ref_one_to_many'] = $this->prepareRef($table['ref_one_to_many'], 'ref_column');
             foreach ($table['ref_one_to_many'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $initializeMethod->addContentLine("\$this->hasMany('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $variableName = lcfirst($aliasModel);
                 $varNameMany = SbUtils::getNameMany(lcfirst($aliasModel));
                 $getMethod = new AbstractClassMethod('get' . SbUtils::getNameMany($aliasModel));
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethodParam1 = new AbstractMethodParam('parameters');
                 $getMethodParam1->setDefaultValue('null');
                 $getMethod->addParam($getMethodParam1);
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}[]");
                 $tableClass->addMethod($getMethod);
                 $addMethod = new AbstractClassMethod('add' . $aliasModel);
                 $addMethod->addContentLine("\$this->_related['{$aliasModel}'][] = \${$variableName};");
                 $addMethod->addContentLine("return \$this;");
                 $addMethodParam1 = new AbstractMethodParam($variableName);
                 $addMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $addMethod->addParam($addMethodParam1);
                 $addMethod->setReturn('$this');
                 $tableClass->addMethod($addMethod);
             }
         }
         if (isset($table['ref_one_to_one'])) {
             $table['ref_one_to_one'] = $this->prepareRef($table['ref_one_to_one'], 'ref_column');
             foreach ($table['ref_one_to_one'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $variableName = lcfirst($aliasModel);
                 $initializeMethod->addContentLine("\$this->hasOne('{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . $aliasModel);
                 $getMethod->addContentLine("\${$variableName} = \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->addContentLine("if (false === \${$variableName}) {");
                 $getMethod->addContentLine(AbstractClass::tab() . "\${$variableName} = new \\{$this->entityNamespace}\\{$ref['model']}();");
                 $getMethod->addContentLine(AbstractClass::tab() . "\${$variableName}->set" . \Phalcon\Text::camelize($ref['ref_column']) . "(\$this->getId());");
                 $getMethod->addContentLine('}');
                 $getMethod->addContentLine("return \${$variableName};");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}");
                 $getMethodParam1 = new AbstractMethodParam('parameters');
                 $getMethodParam1->setDefaultValue('null');
                 $getMethod->addParam($getMethodParam1);
                 $tableClass->addMethod($getMethod);
             }
         }
         if (isset($table['ref_many_to_many'])) {
             $table['ref_many_to_many'] = $this->prepareRefMany($table['ref_many_to_many']);
             foreach ($table['ref_many_to_many'] as $ref) {
                 $aliasModel = $ref['alias'];
                 $variableName = lcfirst($aliasModel);
                 $varNameMany = SbUtils::getNameMany(lcfirst($aliasModel));
                 $intermediateModel = $ref['intermediate_model'];
                 $initializeMethod->addContentLine("\$this->hasManyToMany('{$ref['intermediate_column']}', " . "'{$this->entityNamespace}\\{$ref['intermediate_model']}', '{$ref['intermediate_ref_column']}', " . "'{$ref['column']}', '{$this->entityNamespace}\\{$ref['model']}', '{$ref['ref_column']}', " . "array('alias' => '{$aliasModel}', 'reusable' => true));");
                 $getMethod = new AbstractClassMethod('get' . SbUtils::getNameMany($aliasModel));
                 $getMethod->addContentLine("return \$this->getRelated('{$aliasModel}', \$parameters);");
                 $getMethod->setReturn("\\{$this->entityNamespace}\\{$ref['model']}[]|null");
                 $getMethodParam1 = new AbstractMethodParam("parameters");
                 $getMethodParam1->setDefaultValue("null");
                 $getMethod->addParam($getMethodParam1);
                 $tableClass->addMethod($getMethod);
                 $addMethod = new AbstractClassMethod('add' . $aliasModel);
                 $addMethod->addContentLine("\$this->_related['{$aliasModel}'][] = \${$variableName};");
                 $addMethod->addContentLine("return \$this;");
                 $addMethodParam1 = new AbstractMethodParam($variableName);
                 $addMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $addMethod->addParam($addMethodParam1);
                 $addMethod->setReturn('$this');
                 $tableClass->addMethod($addMethod);
                 $deleteMethod = new AbstractClassMethod('delete' . $aliasModel);
                 $deleteMethod->addContentLine("\$this->{$intermediateModel}->delete(function(\$object) use (\${$variableName}) {");
                 $deleteMethod->addContentLine(AbstractClass::tab() . "/** @var \\{$this->entityNamespace}\\{$intermediateModel} \$object */");
                 $deleteMethod->addContentLine(AbstractClass::tab() . "return \$object->get{$ref['model']}Id() === \${$variableName}->getId();");
                 $deleteMethod->addContentLine('});');
                 $deleteMethod->addContentLine("return \$this;");
                 $deleteMethodParam1 = new AbstractMethodParam($variableName);
                 $deleteMethodParam1->setType("\\{$this->entityNamespace}\\{$ref['model']}");
                 $deleteMethod->addParam($deleteMethodParam1);
                 $deleteMethod->setReturn('$this');
                 $tableClass->addMethod($deleteMethod);
             }
         }
         $tableClass->addMethod($initializeMethod);
         $tableClass->generateSettersAndGetters();
         file_put_contents($this->entityDir . "\\Generated\\" . $table['model'] . '.php', $tableClass);
         if (!is_file($this->entityDir . "\\" . $table['model'] . '.php')) {
             file_put_contents($this->entityDir . "\\" . $table['model'] . '.php', $tableClassChild);
         }
         $modelName = $table['model'] . 'Model';
         if (!is_file($this->modelDir . "\\" . $modelName . '.php')) {
             $modelClass = new AbstractClass($modelName);
             $modelClass->setNamespace('Model');
             $modelClass->setExtends('BasicModel');
             $modelClass->addUse('\\' . $this->entityNamespace . '\\' . $table['model']);
             if (isset($table['primary']) && count($table['primary']) == 1) {
                 $getByIdMethodParam = $table['primary'][0];
                 $getByIdMethod = new AbstractClassMethod("get{$table['model']}By" . \Phalcon\Text::camelize($getByIdMethodParam));
                 $getByIdMethod->addContentLine("return {$table['model']}::findFirst([");
                 $getByIdMethod->addContentLine(AbstractClass::tab() . "'{$table['primary'][0]} = ?1',");
                 $getByIdMethod->addContentLine(AbstractClass::tab() . "'bind' => [1 => \$" . lcfirst(\Phalcon\Text::camelize($getByIdMethodParam)) . "]");
                 $getByIdMethod->addContentLine("]);");
                 $getByIdMethod->setReturn("{$table['model']}");
                 $getMethodParam1 = new AbstractMethodParam(lcfirst(\Phalcon\Text::camelize($getByIdMethodParam)));
                 $getByIdMethod->addParam($getMethodParam1);
                 $modelClass->addMethod($getByIdMethod);
             }
             file_put_contents($this->modelDir . "\\" . $modelName . '.php', $modelClass);
         }
     }
     $metaFile = '.phpstorm.meta.php';
     if (is_file($metaFile)) {
         $metaContent = file_get_contents($metaFile);
         $modelsMeta = '\\Sb\\Phalcon\\Model\\Repository::getModel(\'\') => [' . "\n";
         $modelsMeta .= '            "PageContent\\\\\\PageContent" instanceof \\Model\\PageContent\\PageContentModel,' . "\n";
         foreach ($tables as $table) {
             $modelName = $table['model'];
             $modelsMeta .= "            \"{$modelName}\" instanceof \\Model\\{$modelName}Model,\n";
         }
         $modelsMeta = rtrim($modelsMeta, "\n");
         $modelsMeta .= "\n        ]";
         if (preg_match("#\\\\Sb\\\\Phalcon\\\\Model\\\\Repository::getModel\\(''\\) => \\[(.*?)\\]#is", $metaContent, $m)) {
             $metaContent = preg_replace("#\\\\Sb\\\\Phalcon\\\\Model\\\\Repository::getModel\\(''\\) => \\[(.*?)\\]#is", $modelsMeta, $metaContent);
         }
         file_put_contents($metaFile, $metaContent);
     }
 }
예제 #4
0
 public function getScheme($options = array())
 {
     $connection = $this->getConnection();
     $ignoreTables = array();
     if (array_key_exists('ignore', $options) && is_array($options['ignore'])) {
         $ignoreTables = $options['ignore'];
     }
     $tables = array();
     $tablesList = $connection->listTables();
     foreach ($tablesList as $table) {
         if (in_array($table, $ignoreTables)) {
             continue;
         }
         $columns = array();
         $primary = array();
         $columnsList = $connection->describeColumns($table);
         foreach ($columnsList as $column) {
             $columns[] = $column->getName();
             if ($column->isPrimary()) {
                 $primary[] = $column->getName();
             }
         }
         $tables[$table] = array('name' => $table, 'model' => SbUtils::wordUnderscoreToCamelCase($table), 'columns' => $columns, 'primary' => $primary);
     }
     foreach ($tables as $table => &$tableFields) {
         $refs = $connection->describeReferences($table);
         $indexes = $connection->describeIndexes($table);
         foreach ($indexes as $index) {
             $tableFields['indexes'][] = [$index->getName(), $index->getColumns(), $index->getType()];
         }
         foreach ($refs as $ref) {
             $referencedTable = $ref->getReferencedTable();
             $columns = $ref->getColumns();
             $referencedColumns = $ref->getReferencedColumns();
             $firstReferencedColumn = reset($referencedColumns);
             $firstColumn = reset($columns);
             if (in_array($firstColumn, $tableFields['primary']) && in_array($firstReferencedColumn, $tables[$referencedTable]['primary'])) {
                 $tableFields['ref_one_to_one'][] = array('column' => $firstColumn, 'model' => $tables[$referencedTable]['model'], 'ref_column' => $firstReferencedColumn);
                 $tables[$referencedTable]['ref_one_to_one'][] = array('column' => $firstReferencedColumn, 'model' => $tableFields['model'], 'ref_column' => $firstColumn);
             } else {
                 $tableFields['ref_many_to_one'][] = array('column' => $firstColumn, 'model' => $tables[$referencedTable]['model'], 'ref_column' => $firstReferencedColumn);
                 $tables[$referencedTable]['ref_one_to_many'][] = array('column' => $firstReferencedColumn, 'model' => $tableFields['model'], 'ref_column' => $firstColumn);
             }
         }
     }
     $modelIdx = [];
     foreach ($tables as $tableName => $table) {
         $modelIdx[$table['model']] = $tableName;
     }
     foreach ($tables as $tableName => $table) {
         if (isset($table['ref_one_to_many'])) {
             foreach ($table['ref_one_to_many'] as $oneToManyRef) {
                 $joinModel = $oneToManyRef['model'];
                 $joinTable = $tables[$modelIdx[$joinModel]];
                 if (isset($joinTable['ref_many_to_one'])) {
                     foreach ($joinTable['ref_many_to_one'] as $manyToOneRef) {
                         $secondJoinModel = $manyToOneRef['model'];
                         if ($table['model'] != $secondJoinModel) {
                             if (!array_key_exists('ref_many_to_many', $tables[$tableName])) {
                                 $tables[$tableName]['ref_many_to_many'] = [];
                             }
                             $tables[$tableName]['ref_many_to_many'][] = ['intermediate_column' => $oneToManyRef['column'], 'intermediate_model' => $oneToManyRef['model'], 'intermediate_ref_column' => $oneToManyRef['ref_column'], 'column' => $manyToOneRef['column'], 'model' => $manyToOneRef['model'], 'ref_column' => $manyToOneRef['ref_column']];
                         }
                     }
                 }
             }
         }
     }
     return $tables;
 }