Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
9dc71e4d77 | |||
65bec43b45 | |||
c6806a1c62 | |||
5f3f6b72e5 | |||
6fd19a11be | |||
a1466143b5 | |||
7c727d93e9 | |||
a8d548c0c4 | |||
e02b8c4063 | |||
f03df583d7 | |||
89d1db7a7e | |||
17453427a2 | |||
8dc0a27fd9 | |||
5b1a61cd3b |
@ -37,7 +37,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
}
|
}
|
||||||
return $definitions;
|
return $definitions;
|
||||||
}
|
}
|
||||||
public function parentOf(string $child_model_class, array $relation_definitions): array {
|
public function parentOf(string $child_model_class, array $relation_definitions): ?array {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::CHILD_KEY
|
Model::CHILD_KEY
|
||||||
@ -54,7 +54,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
])
|
])
|
||||||
->many();
|
->many();
|
||||||
}
|
}
|
||||||
public function childOf(string $parent_model_class, array $relation_definitions): ModelInterface {
|
public function childOf(string $parent_model_class, array $relation_definitions): ?ModelInterface {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::PARENT_KEY
|
Model::PARENT_KEY
|
||||||
@ -72,7 +72,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
])
|
])
|
||||||
->one();
|
->one();
|
||||||
}
|
}
|
||||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): array {
|
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::SIBLING_KEY,
|
Model::SIBLING_KEY,
|
||||||
|
@ -22,11 +22,25 @@ class Model {
|
|||||||
}
|
}
|
||||||
return $this;
|
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 !== null) {
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return BaseModel::factory($model_class)->create($data);
|
||||||
|
}
|
||||||
protected $class;
|
protected $class;
|
||||||
public function find(string $model_class): Model {
|
public function find(string $model_class): Model {
|
||||||
if (!class_exists($model_class)) {
|
if (!class_exists($model_class)) {
|
||||||
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
||||||
}
|
}
|
||||||
|
$this->reset();
|
||||||
$this->class = $model_class;
|
$this->class = $model_class;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -328,20 +342,29 @@ class Model {
|
|||||||
return $orm->offset($this->offset);
|
return $orm->offset($this->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function one(): ModelInterface {
|
public function one($id = null): ?ModelInterface {
|
||||||
$result = $this->build()->findOne();
|
$result = $this->build()->findOne($id);
|
||||||
|
if (!$result) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
$result->setFactory($this);
|
$result->setFactory($this);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
public function many(): array {
|
public function many(): ?array {
|
||||||
$results = $this->build()->findMany();
|
$results = $this->build()->findMany();
|
||||||
|
if (!$results) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
foreach ($results as &$r) {
|
foreach ($results as &$r) {
|
||||||
$r->setFactory($this);
|
$r->setFactory($this);
|
||||||
}
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
public function array(): array {
|
public function array(): ?array {
|
||||||
$results = $this->build()->findArray();
|
$results = $this->build()->findArray();
|
||||||
|
if (!$results) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user