/** * @inheritDoc */ public function generate() { $files = array(); $db = $this->getDbConnection(); $modelClass = ModelHelper::generateClassName($db, $db->tablePrefix, $this->subject); $controllerUse = !empty($this->modelNamespace) ? array("{$this->context}\\{$this->modelNamespace}\\{$modelClass}") : array(); $files = array_merge($files, Generator::run(Generator::MODEL, array('subject' => $this->subject, 'context' => $this->context, 'template' => $this->modelTemplate, 'templatePath' => $this->getTemplatePath(), 'baseClass' => $this->modelBaseClass, 'namespace' => $this->modelNamespace)), Generator::run(Generator::CONTROLLER, array('subject' => $this->subject, 'context' => $this->context, 'template' => $this->controllerTemplate, 'templatePath' => $this->getTemplatePath(), 'baseClass' => $this->controllerBaseClass, 'namespace' => $this->controllerNamespace, 'actions' => $this->actions, 'providers' => array(array(Provider::CRUD, 'modelClass' => $modelClass, 'use' => $controllerUse))))); return $files; }
/** * @inheritDoc */ public function generate() { $db = $this->getDbConnection(); $this->tablePrefix = $db->tablePrefix; list($schema, $tables) = $this->resolveSchemaAndTables(); $files = array(); foreach ($tables as $table) { $className = ModelHelper::generateClassName($db, $this->tablePrefix, $table->name); $files[] = new File($this->resolveFilePathByClassName($className), $this->compile(array('className' => $className, 'baseClass' => $this->baseClass, 'namespace' => $this->namespace, 'db' => $this->getDbConnection(), 'schema' => $schema, 'tableSchema' => $table, 'tablePrefix' => $this->tablePrefix, 'buildRelations' => $this->buildRelations, 'commentsAsLabels' => $this->commentsAsLabels))); } return $files; }
/** * @return array */ protected function generateRelations() { if (!$this->buildRelations) { return array(); } $schemaName = ''; if (($pos = strpos($this->tableName, '.')) !== false) { $schemaName = substr($this->tableName, 0, $pos); } $relations = array(); foreach ($this->db->schema->getTables($schemaName) as $table) { if ($this->tablePrefix != '' && strpos($table->name, $this->tablePrefix) !== 0) { continue; } $tableName = $table->name; if ($this->isRelationTable($table)) { $pks = $table->primaryKey; $fks = $table->foreignKeys; $table0 = $fks[$pks[0]][0]; $table1 = $fks[$pks[1]][0]; $className0 = ModelHelper::generateClassName($this->db, $this->tablePrefix, $table0); $className1 = ModelHelper::generateClassName($this->db, $this->tablePrefix, $table1); $unprefixedTableName = ModelHelper::removePrefix($this->db, $this->tablePrefix, $tableName); $relationName = $this->generateRelationName($table0, $table1, true); $relations[$className0][$relationName] = "array(self::MANY_MANY, '{$className1}', '{$unprefixedTableName}({$pks['0']}, {$pks['1']})')"; $relationName = $this->generateRelationName($table1, $table0, true); $i = 1; $rawName = $relationName; while (isset($relations[$className1][$relationName])) { $relationName = $rawName . $i++; } $relations[$className1][$relationName] = "array(self::MANY_MANY, '{$className0}', '{$unprefixedTableName}({$pks['1']}, {$pks['0']})')"; } else { $className = ModelHelper::generateClassName($this->db, $this->tablePrefix, $tableName); foreach ($table->foreignKeys as $fkName => $fkEntry) { // Put table and key name in variables for easier reading $refTable = $fkEntry[0]; // Table name that current fk references to $refKey = $fkEntry[1]; // Key in that table being referenced $refClassName = ModelHelper::generateClassName($this->db, $this->tablePrefix, $refTable); // Add relation for this table $relationName = $this->generateRelationName($tableName, $fkName, false); $relations[$className][$relationName] = "array(self::BELONGS_TO, '{$refClassName}', '{$fkName}')"; // Add relation for the referenced table $relationType = $table->primaryKey === $fkName ? 'HAS_ONE' : 'HAS_MANY'; $relationName = $this->generateRelationName($refTable, ModelHelper::generateClassName($this->db, $this->tablePrefix, $tableName, false), $relationType === 'HAS_MANY'); $i = 1; $rawName = $relationName; while (isset($relations[$refClassName][$relationName])) { $relationName = $rawName . $i++; } $relations[$refClassName][$relationName] = "array(self::{$relationType}, '{$className}', '{$fkName}')"; } } } return $relations; }