Alias models

This commit is contained in:
2021-04-13 23:03:00 -04:00
parent 76205250ad
commit 3651969c33
2 changed files with 92 additions and 0 deletions

View File

@ -38,6 +38,13 @@ class Currency extends Model {
}
return $this->sources;
}
protected $aliases;
public function aliases() {
if ($this->aliases === null) {
$this->aliases = $this->parentOf(Alias::class, [Model::CHILD_KEY => 'currency_id']);
}
return $this->aliases;
}
protected static $fields = ['code', 'name'];
public static function add(ModelFactory $factory, $info) {
@ -73,6 +80,12 @@ class Currency extends Model {
}
return $edited;
}
public function addAlias($info) {
$arr = (array) $info;
$arr['currency_id'] = (int) $this->id;
$result = Alias::add($this->factory, $arr);
return $result;
}
public function addValue($info) {
$arr = (array) $info;
$arr['currency_id'] = (int) $this->id;
@ -85,4 +98,27 @@ class Currency extends Model {
$result = Source::add($this->factory, $arr);
return $result;
}
public static function find(ModelFactory $factory, string $query) {
$query = '%' . $query . '%';
$currency = $factory->find(Currency::class)->where([
['code', $query, 'like']
])->one();
if ($currency !== false and $currency !== null) {
return $currency;
}
$currency = $factory->find(Currency::class)->where([
['name', $query, 'like']
])->one();
if ($currency !== false and $currency !== null) {
return $currency;
}
$alias = $factory->find(Alias::class)->where([
['alias', $query, 'like']
])->one();
if ($alias !== false and $alias !== null) {
return $alias->currency();
}
return false;
}
}