Compare commits
4 Commits
0.1.10
...
6fd19a11be
Author | SHA1 | Date | |
---|---|---|---|
6fd19a11be | |||
a1466143b5 | |||
7c727d93e9 | |||
a8d548c0c4 |
@ -23,6 +23,16 @@ class Model {
|
||||
return $this;
|
||||
}
|
||||
public function create(string $model_class, array $data = null): ModelInterface {
|
||||
if ($data !== null) {
|
||||
$model = $this->find($model_class);
|
||||
foreach ($data as $f => $v) {
|
||||
$model = $model->where([[$f, $v]]);
|
||||
}
|
||||
$model = $model->one();
|
||||
if ($model !== false) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
return BaseModel::factory($model_class)->create($data);
|
||||
}
|
||||
protected $class;
|
||||
@ -332,20 +342,29 @@ class Model {
|
||||
return $orm->offset($this->offset);
|
||||
}
|
||||
|
||||
public function one($id = null): ModelInterface {
|
||||
public function one($id = null): ?ModelInterface {
|
||||
$result = $this->build()->findOne($id);
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
$result->setFactory($this);
|
||||
return $result;
|
||||
}
|
||||
public function many(): array {
|
||||
public function many(): ?array {
|
||||
$results = $this->build()->findMany();
|
||||
if (!$results) {
|
||||
return false;
|
||||
}
|
||||
foreach ($results as &$r) {
|
||||
$r->setFactory($this);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
public function array(): array {
|
||||
public function array(): ?array {
|
||||
$results = $this->build()->findArray();
|
||||
if (!$results) {
|
||||
return false;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user