public function handle()
 {
     $this->checkArgs(array('table'));
     $namespace = array_key_exists('namespace', $this->args) ? $this->args['namespace'] : null;
     if (!isset($namespace)) {
         $namespace = array_key_exists('ns', $this->args) ? $this->args['ns'] : null;
     }
     $table = $this->args['table'];
     $class = array_key_exists('class', $this->args) ? $this->args['class'] : Strings::snakeToCamel($table);
     $path = $this->path($class, $namespace);
     $schema = new TableSchema($table);
     if (file_exists($path)) {
         $content = File::readText($path);
         $className = isset($namespace) ? "{$namespace}\\{$class}" : $class;
         $reflection = new \ReflectionClass($className);
         $header = $reflection->getDocComment();
         if (isset($header)) {
             $content = str_replace($header, Template::generateHeader($schema->columns()), $content);
             unlink($path);
             File::writeText($path, $content);
         }
         Console::line("Model [{$class}] updated in path [{$path}].");
     } else {
         File::writeText($path, Template::generateModel($table, $class, $schema->columns(), $namespace));
         Console::line("Model [{$class}] created in path [{$path}].");
     }
 }
Esempio n. 2
0
 private function parseField(TableSchema $schema, $field)
 {
     if (false === array_search($field, $schema->columns())) {
         return false;
     }
     return $schema->getFieldSymbol($field);
 }
 public function toQueryString(QueryContext $context)
 {
     if (empty($this->inserts)) {
         throw new DataTableException($this->schema->getSymbol(), "Can't execute INSERT without inserts.");
     }
     if ($this->schema->existsField(TableSchema::RESERVED_FIELD_CREATE_AT) && !array_key_exists(TableSchema::RESERVED_FIELD_CREATE_AT, $this->inserts)) {
         $this->inserts[TableSchema::RESERVED_FIELD_CREATE_AT] = time();
     }
     if ($this->schema->existsField(TableSchema::RESERVED_FIELD_UPDATE_AT) && !array_key_exists(TableSchema::RESERVED_FIELD_UPDATE_AT, $this->inserts)) {
         $this->inserts[TableSchema::RESERVED_FIELD_UPDATE_AT] = time();
     }
     $columns = $this->schema->columns();
     foreach ($columns as $item) {
         if (!array_key_exists($item, $this->inserts) && !$this->schema->field($item)->nullable && !$this->schema->field($item)->autoIncrement) {
             throw new FieldValidateException($this->schema->getSymbol(), $item, "Field [{$item}] can't be null.");
         }
     }
     $fields = array();
     $values = array();
     foreach ($this->inserts as $key => $value) {
         if (!$this->schema->existsField($key)) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Not exists field [{$key}] in table [" . $this->schema->getSymbol() . "].");
         }
         $field = $this->schema->field($key);
         if ($field->autoIncrement) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Can't set auto-increment field [{$key}] in insert query.");
         }
         if (ColumnInfo::FIELD_TYPE_ENUM === $field->type) {
             if (false === array_search($value, $field->enums)) {
                 throw new FieldValidateException($this->schema->getSymbol(), $key, "Not exists enum value [{$value}] in insert query. " . "The value must be one of (" . implode(',', $field->enums) . ").");
             }
         }
         if (isset($field->validation) && !$field->validation->valid($value)) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Field value validation error. [{$key}]");
         }
         $fields[] = "`{$key}`";
         $values[] = "?";
         $context->param($value);
     }
     $statements = array("INSERT INTO", $this->schema->getSymbol(), "(", implode(',', $fields), ")VALUES(", implode(',', $values), ")");
     return implode(' ', $statements);
 }
Esempio n. 4
0
 private function load()
 {
     if (isset($this->query)) {
         $result = $this->query->execute();
         if (!empty($result)) {
             if (count($result) > 1) {
                 throw new EntityException("More than one row in table query.");
             }
             $row = $result[0];
             foreach ($row as $key => $value) {
                 $this->fields[$key] = $value;
             }
             $this->shadows = $row;
             $this->exists = true;
         } else {
             $this->exists = false;
             foreach ($this->schema->columns() as $field) {
                 $this->shadows[$field] = null;
             }
         }
     }
 }