Rozwiązaliśmy to teraz ogólnie dla wszystkich modeli, rozszerzając nasz model podstawowy o następującą funkcjonalność:
- Definiujemy tablicę atrybutów, które przechowują dane geometryczne.
- Decydujemy na podstawie modelu, jeśli chcemy, aby to było automatycznie ładowane jako tekst.
- Zmieniamy domyślny kreator zapytań, aby wybrać atrybuty geometrii jako tekst z bazy danych.
Oto fragment modelu podstawowego, którego teraz używamy:
/**
* The attributes that hold geometrical data.
*
* @var array
*/
protected $geometry = array();
/**
* Select geometrical attributes as text from database.
*
* @var bool
*/
protected $geometryAsText = false;
/**
* Get a new query builder for the model's table.
* Manipulate in case we need to convert geometrical fields to text.
*
* @param bool $excludeDeleted
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newQuery($excludeDeleted = true)
{
if (!empty($this->geometry) && $this->geometryAsText === true)
{
$raw = '';
foreach ($this->geometry as $column)
{
$raw .= 'AsText(`' . $this->table . '`.`' . $column . '`) as `' . $column . '`, ';
}
$raw = substr($raw, 0, -2);
return parent::newQuery($excludeDeleted)->addSelect('*', DB::raw($raw));
}
return parent::newQuery($excludeDeleted);
}