public function run($parameters) { $name = $this->getOption(array('name', 1)); $className = Text::camelize(isset($parameters[2]) ? $parameters[2] : $name); $fileName = Text::uncamelize($className); $schema = $this->getOption('schema'); $modelBuilder = new \Phalcon\Builder\Model(array('name' => $name, 'schema' => $schema, 'className' => $className, 'fileName' => $fileName, 'genSettersGetters' => $this->isReceivedOption('get-set'), 'genDocMethods' => $this->isReceivedOption('doc'), 'namespace' => $this->getOption('namespace'), 'directory' => $this->getOption('directory'), 'force' => $this->isReceivedOption('force'))); $modelBuilder->build(); }
public function build() { $options = $this->_options; $path = ''; if (isset($this->_options['directory'])) { if ($this->_options['directory']) { $path = $this->_options['directory'] . '/'; } } $name = $options['name']; $config = $this->_getConfig($path); if (!isset($config->database->adapter)) { throw new BuilderException("Adapter was not found in the config. Please specify a config varaible [database][adapter]"); } $adapter = ucfirst($config->database->adapter); $this->isSupportedAdapter($adapter); $di = new \Phalcon\DI\FactoryDefault(); $di->set('db', function () use($adapter, $config) { $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; $connection = new $adapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->name)); return $connection; }); if (isset($config->application->modelsDir)) { $options['modelsDir'] = $path . $config->application->modelsDir; } else { throw new BuilderException("The builder is unable to know where is the views directory"); } if (isset($config->application->controllersDir)) { $options['controllersDir'] = $path . $config->application->controllersDir; } else { throw new BuilderException("The builder is unable to know where is the controllers directory"); } if (isset($config->application->viewsDir)) { $options['viewsDir'] = $path . $config->application->viewsDir; } else { throw new BuilderException("The builder is unable to know where is the views directory"); } $options['manager'] = $di->getShared('modelsManager'); $options['className'] = Text::camelize($options['name']); $options['fileName'] = Text::uncamelize($options['className']); $modelBuilder = new \Phalcon\Builder\Model(array('name' => $name, 'schema' => $options['schema'], 'className' => $options['className'], 'fileName' => $options['fileName'], 'genSettersGetters' => $options['genSettersGetters'], 'directory' => $options['directory'], 'force' => $options['force'])); $modelBuilder->build(); $modelClass = Text::camelize($name); if (!class_exists($modelClass)) { require $config->application->modelsDir . '/' . $modelClass . '.php'; } $entity = new $modelClass(); $metaData = $di->getShared('modelsMetadata'); $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = array(); $selectDefinition = array(); $relationField = ''; $single = $name; $options['name'] = strtolower(Text::camelize($single)); $options['plural'] = str_replace('_', ' ', $single); $options['single'] = str_replace('_', ' ', $single); $options['entity'] = $entity; $options['theSingle'] = $single; $options['singleVar'] = $single; $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($path, $options); //View layouts $this->_makeLayouts($path, $options); //View index.phtml $this->_makeViewIndex($path, $options); //View search.phtml $this->_makeViewSearch($path, $options); //View new.phtml $this->_makeViewNew($path, $options); //View edit.phtml $this->_makeViewEdit($path, $options); return true; }
public function build() { $path = '.'; if (isset($this->_options['directory'])) { $path = $this->_options['directory']; } $config = $this->_getConfig($path . '/'); $modelsDir = $config->application->modelsDir; $forceProcess = $this->_options['force']; 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; } $adapter = $config->database->adapter; $this->isSupportedAdapter($adapter); $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; $db = new $adapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'name' => $config->database->name)); $initialize = array(); if (isset($this->_options['schema'])) { if ($this->_options['schema'] != $db->getDatabaseName()) { $initialize[] = "\t\t\$this->setSchema(\"{$this->_options['schema']}\");"; } $schema = $this->_options['schema']; } else { $schema = $config->database->name; } $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(); } foreach ($db->tableOptions($name, $schema) as $field) { if (preg_match('/([a-z0-9_]+)_id$/', $field['Field'], $matches)) { if ($defineRelations) { $hasMany[$matches[1]][Utils::camelize($name)] = array('fields' => 'id', 'relationFields' => $field['Field']); $belongsTo[$name][Utils::camelize($matches[1])] = array('fields' => $field['Field'], 'relationFields' => 'id'); } if ($defineForeignKeys) { $foreignKeys[$name][] = array('fields' => $field['Field'], 'entity' => Utils::camelize($matches[1]), 'referencedFields' => 'id'); } } } $camelizedName = Utils::camelize($name); foreach ($db->describeReferences($name, $schema) as $reference) { $columns = $reference->getColumns(); $referencedColumns = $reference->getReferencedColumns(); $referencedModel = Utils::camelize($reference->getReferencedTable()); if ($defineRelations) { if ($reference->getReferencedSchema() == $schema) { if (count($columns) == 1) { $belongsTo[$name][$referencedModel] = array('fields' => $columns[0], 'relationFields' => $referencedColumns[0]); $hasMany[$reference->getReferencedTable()][$camelizedName] = array('fields' => $referencedColumns[0], 'relationFields' => $columns[0]); } } } if ($defineForeignKeys) { if ($reference->getReferencedSchema() == $schema) { if (count($columns) == 1) { $foreignKeys[$name][] = array('fields' => $columns[0], 'entity' => $referencedModel, 'referencedFields' => $referencedColumns[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 = Utils::camelize($name); if (!file_exists($modelsDir . '/' . $className . '.php') || $forceProcess) { 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 \Phalcon\Builder\Model(array('name' => $name, 'schema' => $schema, 'force' => $forceProcess, 'hasMany' => $hasManyModel, 'belongsTo' => $belongsToModel, 'foreignKeys' => $foreignKeysModel, 'genSettersGetters' => $genSettersGetters, 'directory' => $this->_options['directory'])); $modelBuilder->build(); } else { echo "INFO: Skip model \"{$name}\" because it already exist\n"; } } }
public function build() { $path = '.'; if (isset($this->_options['directory'])) { $path = $this->_options['directory']; } $config = $this->_getConfig($path . '/'); $modelsDir = $config->application->modelsDir; $forceProcess = $this->_options['force']; 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; } $adapter = $config->database->adapter; $this->isSupportedAdapter($adapter); if (isset($config->database->adapter)) { $adapter = $config->database->adapter; } else { $adapter = 'Mysql'; } if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { $configArray = $config->database; } $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($config->database->schema) ? $config->database->schema : $config->database->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 = Utils::camelize($name); $refSchema = $adapter != 'Postgresql' ? $schema : $config->database->dbname; foreach ($db->describeReferences($name, $schema) as $reference) { $columns = $reference->getColumns(); $referencedColumns = $reference->getReferencedColumns(); $referencedModel = Utils::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 = Utils::camelize($name); if (!file_exists($modelsDir . '/' . $className . '.php') || $forceProcess) { 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 \Phalcon\Builder\Model(array('name' => $name, 'schema' => $schema, 'extends' => isset($this->_options['extends']) ? $this->_options['extends'] : null, 'namespace' => $this->_options['namespace'], 'force' => $forceProcess, 'hasMany' => $hasManyModel, 'belongsTo' => $belongsToModel, 'foreignKeys' => $foreignKeysModel, 'genSettersGetters' => $genSettersGetters, 'directory' => $this->_options['directory'])); $modelBuilder->build(); } else { if ($this->isConsole()) { print Color::info("Skipping model \"{$name}\" because it already exist"); } else { $this->exist[] = $name; } } } }