public function build() { // Check name (table name) if (!$this->_options['table_name']) { throw new BuilderException("You must specify the table name"); } // Get config $path = ''; if (isset($this->_options['config_path'])) { $path = $this->_options['config_path']; } elseif (isset($this->_options['app_path'])) { $path = $this->_options['app_path']; } elseif (isset($this->_options['module_path'])) { $path = $this->_options['module_path']; } $config = $this->_getConfig($path); // build options $this->buildOptions($this->_options['table_name'], $config); // Prepare DB connection if (!$this->prepareDbConnection($config)) { return false; } // Check if table exist in database $table = $this->_options['table_name']; if ($this->db->tableExists($table, $config->database->dbname)) { $fields = $this->db->describeColumns($table, $config->database->dbname); } else { throw new BuilderException('Table "' . $table . '" does not exists'); } // Set extender class template switch ($this->type) { case self::TYPE_SIMPLE: case self::TYPE_EXTJS: default: $extends = $this->templateSimpleModelExtends; break; } $attributes = array(); $belongsTo = array(); foreach ($fields as $field) { $type = $this->getPHPType($field->getType()); $attributes[] = sprintf($this->templateEmptyAttribute, $type, 'public', $field->getName()); // Build belongsTo relations preg_match('/^(.*)\\_i{1}d{1}$/', $field->getName(), $matches); if (!empty($matches)) { $belongsTo[] = sprintf($this->templateModelRelation, 'belongsTo', $matches[0], ucfirst($this->_builderOptions['moduleName']) . '\\Model\\' . Inflector::modelize($matches[1]), 'id', $this->_buildRelationOptions(['alias' => $this->getAlias($matches[1])])); } if ($field->getName() == 'id' || $field->isPrimary()) { $this->_options['primary_column'] = $field->getName(); $this->_options['order_expr'] = $field->getName(); } if ($field->getName() == 'title' || $field->getName() == 'name') { $this->_options['name_expr'] = $field->getName(); $this->_options['order_expr'] = $field->getName(); } } // Model::initialize() code $initializeCode = ""; if (count($belongsTo) > 0) { foreach ($belongsTo as $rel) { $initializeCode .= $rel . "\n"; } } // Join attributes to content $content = join('', $attributes); // Join engine properties if (isset($this->_options['primary_column'])) { $content .= sprintf($this->templateModelPrimaryColumn, $this->_options['primary_column']); } // Join engine name_expr if (isset($this->_options['name_expr'])) { $content .= sprintf($this->templateModelDefaultTitleColumn, $this->_options['name_expr']); } // Join engine attributes if (isset($this->_options['attributes']) && is_array($this->_options['attributes'])) { $content .= sprintf($this->templateModelAttribute, $this->_options['attributes']); } // Join engine orderExpr if (isset($this->_options['order_expr'])) { $content .= sprintf($this->templateModelOrderExpr, $this->_options['order_expr']); } // Join engine orderAsc if (isset($this->_options['order_asc']) && is_bool($this->_options['order_asc'])) { $content .= sprintf($this->templateModelOrder, $this->_options['order_asc']); } else { $content .= sprintf($this->templateModelOrder, 'true'); } // Join initialize code to content $content .= ''; if (!empty($initializeCode)) { $content .= sprintf($this->templateInitialize, $initializeCode); } // Join Model::getSource() code to content $content .= sprintf($this->templateModelGetSource, $this->_options['table_name']); if (isset($this->_options['mapColumn'])) { $content .= $this->_genColumnMapCode($fields); } $code = sprintf($this->templateClassFullStack, '', $this->_builderOptions['namespace'], $this->_builderOptions['use'], $this->_builderOptions['head'], $this->_builderOptions['className'], $extends, $content); file_put_contents($this->_builderOptions['path'], $code); print Color::success('Model "' . $this->_builderOptions['className'] . '" was successfully created.') . PHP_EOL; }