/** * Generate models */ public function createAction() { if ($this->request->isPost()) { $name = $this->request->getPost('name', 'string'); $module = $this->request->getPost('module', 'string'); $force = $this->request->getPost('force', 'int'); $schema = $this->request->getPost('schema', 'string'); $directory = $this->request->getPost('directory', 'string'); $namespace = $this->request->getPost('namespace', 'string'); $baseClass = $this->request->getPost('baseClass', 'string'); $tableName = $this->request->getPost('tableName', 'string'); $genSettersGetters = $this->request->getPost('genSettersGetters', 'int'); $foreignKeys = $this->request->getPost('foreignKeys', 'int'); $defineRelations = $this->request->getPost('defineRelations', 'int'); try { $component = array('module' => $module, 'name' => $name, 'baseClass' => $baseClass, 'tableName' => $tableName, 'schema' => $schema, 'force' => $force, 'directory' => $directory, 'foreignKeys' => $foreignKeys, 'defineRelations' => $defineRelations, 'genSettersGetters' => $genSettersGetters, 'namespace' => $namespace); if ($tableName == 'all') { $modelBuilder = new AllModels($component); } else { $modelBuilder = new Model($component); } $modelBuilder->build(); if ($tableName == 'all') { if (($n = count($modelBuilder->exist)) > 0) { $mList = implode('</strong>, <strong>', $modelBuilder->exist); if ($n == 1) { $notice = 'Model <strong>' . $mList . '</strong> was skipped because it already exists!'; } else { $notice = 'Models <strong>' . $mList . '</strong> were skipped because they already exists!'; } $this->flash->notice($notice); } } if ($tableName == 'all') { $this->flash->success('Models were created successfully'); } else { $this->flash->success('Model "' . $tableName . '" was created successfully'); } } catch (\Exception $e) { $this->flash->error($e->getMessage()); } } $this->dispatcher->forward(array('action' => 'index')); }
/** * Build Models * @throws \Exception */ public function build() { if (isset($this->_options['defineRelations'])) { $defineRelations = $this->_options['defineRelations']; } else { $defineRelations = false; } if (isset($this->_options['foreignKeys'])) { $defineForeignKeys = $this->_options['foreignKeys']; } else { $defineForeignKeys = false; } if (isset($this->_options['genSettersGetters'])) { $genSettersGetters = $this->_options['genSettersGetters']; } else { $genSettersGetters = false; } if (isset(Tools::getConfig()->database)) { $dbConfig = Tools::getConfig()->database; } elseif (Tools::getConfig()->db) { $dbConfig = Tools::getConfig()->db; } if (isset($dbConfig->adapter)) { $adapter = $dbConfig->adapter; $this->isSupportedAdapter($adapter); } else { $adapter = 'Mysql'; } if (is_object($dbConfig)) { $configArray = $dbConfig->toArray(); } else { $configArray = $dbConfig; } $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; unset($configArray['adapter']); /** * @var $db \Phalcon\Db\Adapter\Pdo */ $db = new $adapterName($configArray); if (isset($this->_options['schema'])) { $schema = $this->_options['schema']; } elseif ($adapter == 'Postgresql') { $schema = 'public'; } else { $schema = isset($dbConfig->schema) ? $dbConfig->schema : $dbConfig->dbname; } $hasMany = array(); $belongsTo = array(); $foreignKeys = array(); if ($defineRelations || $defineForeignKeys) { foreach ($db->listTables($schema) as $name) { if ($defineRelations) { if (!isset($hasMany[$name])) { $hasMany[$name] = array(); } if (!isset($belongsTo[$name])) { $belongsTo[$name] = array(); } } if ($defineForeignKeys) { $foreignKeys[$name] = array(); } $camelCaseName = Text::camelize($name); $refSchema = $adapter != 'Postgresql' ? $schema : $dbConfig->dbname; foreach ($db->describeReferences($name, $schema) as $reference) { $columns = $reference->getColumns(); $referencedColumns = $reference->getReferencedColumns(); $referencedModel = Text::camelize($reference->getReferencedTable()); if ($defineRelations) { if ($reference->getReferencedSchema() == $refSchema) { if (count($columns) == 1) { $belongsTo[$name][] = array('referencedModel' => $referencedModel, 'fields' => $columns[0], 'relationFields' => $referencedColumns[0], 'options' => $defineForeignKeys ? array('foreignKey' => true) : NULL); $hasMany[$reference->getReferencedTable()][] = array('camelizedName' => $camelCaseName, 'fields' => $referencedColumns[0], 'relationFields' => $columns[0]); } } } } } } else { foreach ($db->listTables($schema) as $name) { if ($defineRelations) { $hasMany[$name] = array(); $belongsTo[$name] = array(); $foreignKeys[$name] = array(); } } } foreach ($db->listTables($schema) as $name) { $className = Text::camelize($name); if (!file_exists($this->_options['directory'] . DIRECTORY_SEPARATOR . $className . '.php') || $this->_options['force']) { if (isset($hasMany[$name])) { $hasManyModel = $hasMany[$name]; } else { $hasManyModel = array(); } if (isset($belongsTo[$name])) { $belongsToModel = $belongsTo[$name]; } else { $belongsToModel = array(); } if (isset($foreignKeys[$name])) { $foreignKeysModel = $foreignKeys[$name]; } else { $foreignKeysModel = array(); } $modelBuilder = new Model(array('module' => $this->_options['module'], 'name' => $className, 'tableName' => $name, 'schema' => $schema, 'baseClass' => $this->_options['baseClass'], 'namespace' => $this->_options['namespace'], 'force' => $this->_options['force'], 'hasMany' => $hasManyModel, 'belongsTo' => $belongsToModel, 'foreignKeys' => $foreignKeysModel, 'genSettersGetters' => $genSettersGetters, 'directory' => $this->_options['directory'])); $modelBuilder->build(); } else { $this->exist[] = $className; } } }
/** * @return bool * @throws \Exception */ public function build() { $options = $this->_options; if (Tools::getDb()) { $config = Tools::getDb(); } if (!isset($config->adapter)) { throw new \Exception("Adapter was not found in the config. Please specify a config variable [database][adapter]"); } $adapter = ucfirst($config->adapter); $this->isSupportedAdapter($adapter); $di = new FactoryDefault(); $di->set('db', function () use($adapter, $config) { if (isset($config->adapter)) { $adapter = $config->adapter; } else { $adapter = 'Mysql'; } if (is_object($config)) { $configArray = $config->toArray(); } else { $configArray = $config; } $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; unset($configArray['adapter']); return new $adapterName($configArray); }); $options['manager'] = $di->getShared('modelsManager'); $options['className'] = $options['name']; $options['fileName'] = str_replace('_', '-', Text::uncamelize($options['className'])); $modelsNamespace = $options['modelsNamespace']; if (isset($modelsNamespace) && substr($modelsNamespace, -1) !== '\\') { $modelsNamespace .= "\\"; } $modelName = $options['name']; $modelClass = $modelsNamespace . $modelName; if (!@dir($options['modelsDir'])) { if (!@mkdir($options['modelsDir'])) { throw new \Exception('Could not create directory on ' . $options['modelsDir']); } @chmod($options['modelsDir'], 0777); } $modelPath = $options['modelsDir'] . DIRECTORY_SEPARATOR . $modelName . '.php'; if (!file_exists($modelPath)) { $modelBuilder = new ModelBuilder(array('module' => $options['module'], 'name' => null, 'tableName' => $options['tableName'], 'schema' => $options['schema'], 'baseClass' => null, 'namespace' => $options['modelsNamespace'], 'foreignKeys' => true, 'defineRelations' => true, 'genSettersGetters' => $options['genSettersGetters'], 'directory' => $options['modelsDir'], 'force' => $options['force'])); $modelBuilder->build(); } if (!class_exists($modelClass)) { require_once $modelPath; } $entity = new $modelClass(); $metaData = $di['modelsMetadata']; $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = array(); $selectDefinition = array(); $relationField = ''; $options['name'] = Text::uncamelize($options['name']); $options['plural'] = $this->_getPossiblePlural($options['name']); $options['singular'] = $this->_getPossibleSingular($options['name']); $options['modelClass'] = $options['modelsNamespace'] . '\\' . $this->_options['name']; $options['entity'] = $entity; $options['setParams'] = $setParams; $options['attributes'] = $attributes; $options['dataTypes'] = $dataTypes; $options['primaryKeys'] = $primaryKeys; $options['identityField'] = $identityField; $options['relationField'] = $relationField; $options['selectDefinition'] = $selectDefinition; $options['autocompleteFields'] = array(); $options['belongsToDefinitions'] = array(); //Build Controller $this->_makeController($options); if (isset($options['templateEngine']) && $options['templateEngine'] == 'volt') { //View layouts // $this->_makeLayoutsVolt($options); //View index.phtml $this->_makeViewIndexVolt(null, $options); //View search.phtml $this->_makeViewSearchVolt(null, $options); //View new.phtml $this->_makeViewNewVolt(null, $options); //View edit.phtml $this->_makeViewEditVolt(null, $options); } else { //View layouts // $this->_makeLayouts(null, $options); //View index.phtml $this->_makeViewIndex(null, $options); //View search.phtml $this->_makeViewSearch(null, $options); //View new.phtml $this->_makeViewNew(null, $options); //View edit.phtml $this->_makeViewEdit(null, $options); } return true; }