Ejemplo n.º 1
0
 /**
  * Cast (specify) index shema in associated table based on Record index property definition.
  * Only normal or unique indexes can be casted at this moment.
  *
  * Example:
  * protected $indexes = array(
  *      [self::UNIQUE, 'email'],
  *      [self::INDEX, 'status', 'balance'],
  *      [self::INDEX, 'public_id']
  * );
  *
  * @param array $definition
  * @return AbstractIndex
  * @throws DefinitionException
  * @throws \Spiral\Database\Exceptions\SchemaException
  */
 protected function castIndex(array $definition)
 {
     //Index type (UNIQUE or INDEX)
     $type = null;
     //Columns index associated too
     $columns = [];
     foreach ($definition as $chunk) {
         if ($chunk == RecordEntity::INDEX || $chunk == RecordEntity::UNIQUE) {
             $type = $chunk;
             continue;
         }
         if (!$this->tableSchema->hasColumn($chunk)) {
             throw new DefinitionException("Record '{$this}' has index definition with undefined local column.");
         }
         $columns[] = $chunk;
     }
     if (empty($type)) {
         throw new DefinitionException("Record '{$this}' has index definition with unspecified index type.");
     }
     if (empty($columns)) {
         throw new DefinitionException("Record '{$this}' has index definition without any column associated to.");
     }
     //Casting schema
     return $this->tableSchema->index($columns)->unique($type == RecordEntity::UNIQUE);
 }