Ejemplo n.º 1
0
 /**
  * Method utilizes value of record schema property to generate table columns. Property "indexes"
  * going to feed table indexes.
  *
  * @see Record::$schema
  * @throws DefinitionException
  * @throws \Spiral\Database\Exceptions\SchemaException
  */
 public function castSchema()
 {
     //Default values fetched from record, system will try to use this values as default
     //values for associated table column
     $defaults = $this->property('defaults', true);
     foreach ($this->property('schema', true) as $name => $definition) {
         if (is_array($definition)) {
             //Relation or something else
             continue;
         }
         //Let's cast table column using it's name, declared definition and default value (if any)
         $this->castColumn($this->tableSchema->column($name), $definition, isset($defaults[$name]) ? $defaults[$name] : null);
     }
     //Casting declared record indexes
     foreach ($this->getIndexes() as $definition) {
         $this->castIndex($definition);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param AbstractTable $schema
  * @return AbstractColumn
  * @throws ColumnException
  */
 protected function declareColumn(AbstractTable $schema)
 {
     $column = $schema->column($this->name);
     //Type configuring
     if (method_exists($column, $this->type)) {
         $arguments = [];
         $method = new \ReflectionMethod($column, $this->type);
         foreach ($method->getParameters() as $parameter) {
             if ($this->hasOption($parameter->getName())) {
                 $arguments[] = $this->getOption($parameter->getName());
             } elseif (!$parameter->isOptional()) {
                 throw new ColumnException("Option '{$parameter->getName()}' are required to define column with type '{$this->type}'");
             } else {
                 $arguments[] = $parameter->getDefaultValue();
             }
         }
         call_user_func_array([$column, $this->type], $arguments);
     } else {
         $column->setType($this->type);
     }
     $column->nullable($this->getOption('nullable', false));
     $column->defaultValue($this->getOption('default', null));
     return $column;
 }