static function toCode(ModelDescriptor $descriptor) { $classFile = new CodeGenClassFile(); $class = new CodeGenClass($descriptor->modelClass); $class->setExtends('Model'); $classFile->addClass($class); $classDocComment = new CodeGenDocComment(); $classDocComment->addLine('!Database ' . $descriptor->source); $classDocComment->addLine('!Table ' . $descriptor->getTable()); $class->setDocComment($classDocComment); foreach ($descriptor->properties as $prop) { $property = new CodeGenProperty($prop->name); $columnDocComment = '!Column '; if ($prop->isPrimaryKey) { $columnDocComment .= 'PrimaryKey, '; } $columnDocComment .= $prop->type; if ($prop->isAutoIncrement) { $columnDocComment .= ', AutoIncrement'; } $propertyDocComment = new CodeGenDocComment($columnDocComment); $property->setDocComment($propertyDocComment); $class->addProperty($property); } return $classFile->toCode(); }
/** * Transform a model descriptor to a table descriptor. * * @param ModelDescriptor $descriptor * @return RecessTableDescriptor */ function modelToTableDescriptor(ModelDescriptor $descriptor) { Library::import('recess.database.pdo.RecessTableDescriptor'); Library::import('recess.database.pdo.RecessColumnDescriptor'); $tableDescriptor = new RecessTableDescriptor(); $tableDescriptor->name = $descriptor->getTable(); foreach ($descriptor->properties as $property) { $tableDescriptor->addColumn($property->name, $property->type, true, $property->isPrimaryKey, array(), $property->isAutoIncrement ? array('autoincrement' => true) : array()); } return $tableDescriptor; }
function attachMethodsToModelDescriptor(ModelDescriptor $descriptor) { $alias = $this->name; $attachedMethod = new AttachedMethod($this, 'select', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); $alias = 'set' . ucfirst($this->name); $attachedMethod = new AttachedMethod($this, 'set', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); $alias = 'unset' . ucfirst($this->name); $attachedMethod = new AttachedMethod($this, 'remove', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); }
function attachMethodsToModelDescriptor(ModelDescriptor &$descriptor) { $alias = $this->name; $attachedMethod = new AttachedMethod($this, 'selectModel', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); $alias = 'addTo' . ucfirst($this->name); $attachedMethod = new AttachedMethod($this, 'addTo', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); $alias = 'removeFrom' . ucfirst($this->name); $attachedMethod = new AttachedMethod($this, 'removeFrom', $alias); $descriptor->addAttachedMethod($alias, $attachedMethod); }
/** * Return a SqlBuilder object which has set the table and optionally * assigned values to columns based on this instances' properties. This is used in * insert(), update(), and delete() * * @param ModelDescriptor $descriptor * @param boolean $useAssignment * @param boolean $excludePrimaryKey * @return SqlBuilder */ protected function assignmentSqlForThisObject(ModelDescriptor $descriptor, $useAssignment = true, $excludePrimaryKey = false) { $sqlBuilder = new SqlBuilder(); $sqlBuilder->from($descriptor->getTable()); if (empty($descriptor->columns)) { throw new RecessException('The "' . $descriptor->getTable() . '" table does not appear to exist in your database.', get_defined_vars()); } foreach ($this as $column => $value) { if ($excludePrimaryKey && $descriptor->primaryKey == $column) { continue; } if (in_array($column, $descriptor->columns) && isset($value)) { if ($useAssignment) { $sqlBuilder->assign($column, $value); } else { $sqlBuilder->equal($column, $value); } } } return $sqlBuilder; }
/** !Route POST, app/$app/model/gen */ public function generateModel($app) { $values = $this->request->post; $modelName = $values['modelName']; $tableExists = $values['tableExists'] == 'yes' ? true : false; if ($tableExists) { $dataSource = $values['existingDataSource']; $createTable = false; $tableName = $values['existingTableName']; } else { $dataSource = $values['dataSource']; $createTable = $values['createTable'] == 'Yes' ? true : false; $tableName = $values['tableName']; } $propertyNames = $values['fields']; $primaryKey = $values['primaryKey']; $types = $values['types']; Library::import('recess.database.orm.Model', true); // Forcing b/c ModelDescriptor is in Model $modelDescriptor = new ModelDescriptor($modelName, false); $modelDescriptor->setSource($dataSource); $modelDescriptor->setTable($tableName, false); $pkFound = false; foreach ($propertyNames as $i => $name) { if ($name == "") { continue; } $property = new ModelProperty(); $property->name = trim($name); if ($name == $primaryKey) { $property->isPrimaryKey = true; } if ($types[$i] == 'Integer Autoincrement') { if ($property->isPrimaryKey) { $property->type = RecessType::INTEGER; $property->isAutoIncrement = true; } else { $property->type = RecessType::INTEGER; } } else { $property->type = $types[$i]; } $modelDescriptor->properties[] = $property; } Library::import('recess.database.orm.ModelGen'); $this->modelCode = ModelGen::toCode($modelDescriptor, $_ENV['dir.temp'] . 'Model.class.php'); $app = new $app(); if (strpos($app->modelsPrefix, 'recess.apps.') !== false) { $base = $_ENV['dir.recess']; } else { $base = $_ENV['dir.apps']; } $path = $base . str_replace(Library::dotSeparator, Library::pathSeparator, $app->modelsPrefix); $path .= $modelName . '.class.php'; $this->path = $path; $this->modelWasSaved = false; $this->codeGenMessage = ''; try { if (file_exists($this->path)) { if (file_get_contents($this->path) == $this->modelCode) { $this->modelWasSaved = true; } else { $this->codeGenMessage = 'File already exists!'; } } else { file_put_contents($this->path, $this->modelCode); $this->modelWasSaved = true; } } catch (Exception $e) { $this->codeGenMessage = 'File could not be saved. Is models directory writeable?'; $this->modelWasSaved = false; } $this->modelName = $modelName; $this->appName = get_class($app); $this->tableGenAttempted = $createTable; $this->tableWasCreated = false; $this->tableSql = ''; if ($createTable) { $modelSource = Databases::getSource($dataSource); $this->tableSql = $modelSource->createTableSql($modelDescriptor); try { $modelSource->exec($this->tableSql); $this->tableWasCreated = true; } catch (Exception $e) { $this->tableWasCreated = false; } } return $this->ok('createModelComplete'); }