Skip to content

Commit de262de

Browse files
committed
Add more entity functionality.
1 parent f4e0ec3 commit de262de

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

src/EntityModel.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
class EntityModel extends ModelAbstract
66
{
7+
const STATE_NEW = 1;
8+
9+
const STATE_MANAGED = 2;
10+
711
protected $entityClass;
812

13+
private $entityStates = [];
14+
915
public function entityClass()
1016
{
1117
if (!$this->entityClass) {
@@ -31,6 +37,62 @@ public function create(array $record = [])
3137

3238
public function save($entity)
3339
{
40+
$entityClass = $this->entityClass();
41+
42+
if (!$entity instanceof $entityClass) {
43+
throw new \Exception('Entity should be an instance of `' . $entityClass . '`.');
44+
}
45+
46+
$record = [];
47+
48+
(function () use (&$record) {
49+
$record = get_object_vars($this);
50+
})->call($entity);
51+
52+
switch($this->getEntityState($entity)) {
53+
case self::STATE_NEW:
54+
$this->insert($record);
55+
56+
if ($autoIncrement = $this->autoIncrement()) {
57+
$id = (int) $this->connection()->lastInsertId();
58+
59+
(function () use ($autoIncrement, $id) {
60+
$this->{$autoIncrement} = $id;
61+
})->call($entity);
62+
}
63+
64+
$this->setEntityState($entity, self::STATE_MANAGED);
65+
66+
break;
67+
case self::STATE_MANAGED:
68+
$keys = [];
69+
70+
foreach ($this->firstUniqueKey() as $key) {
71+
$keys[$key] = $record[$key];
72+
}
73+
74+
$query = $this->newUpdateQuery()
75+
->setMultiple(array_diff_key($record, $keys))
76+
->whereMultiple($keys);
77+
78+
$this->connection()->sqlExecute(...$query->toSql());
79+
80+
break;
81+
}
82+
83+
return $this;
84+
}
85+
86+
private function setEntityState($entity, $state)
87+
{
88+
$this->entityStates[spl_object_hash($entity)] = $state;
89+
90+
return $this;
91+
}
92+
93+
private function getEntityState($entity)
94+
{
95+
return $this->entityStates[spl_object_hash($entity)] ?? self::STATE_NEW;
3496
}
3597

3698
protected function prepareRowInstance(array $record)

src/Model.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function serialize()
2727
$this->autoIncrement,
2828
$this->uniqueKeys,
2929
$this->casts,
30-
$this->customRecord,
3130

3231
$this->fillable,
3332
$this->guarded,
@@ -46,7 +45,6 @@ public function unserialize($serialized)
4645
$this->autoIncrement,
4746
$this->uniqueKeys,
4847
$this->casts,
49-
$this->customRecord,
5048

5149
$this->fillable,
5250
$this->guarded,

src/ModelAbstract.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public function serialize()
8181
$this->primaryKey,
8282
$this->autoIncrement,
8383
$this->uniqueKeys,
84-
$this->casts,
85-
$this->customRecord,
8684
]);
8785
}
8886

@@ -93,8 +91,6 @@ public function unserialize($serialized)
9391
$this->primaryKey,
9492
$this->autoIncrement,
9593
$this->uniqueKeys,
96-
$this->casts,
97-
$this->customRecord,
9894
] = unserialize($serialized);
9995
}
10096

src/RowsTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,13 @@ public function save(array $values = [])
394394

395395
foreach ($this->rows as $key => &$row) {
396396
$data = $row;
397+
397398
if ($this->rowStateIsCasted($key)) {
398399
$data = $this->uncastRecord($data);
399400
}
400401

401402
if ($this->rowStateIsNew($key)) {
402-
$query = $this->newInsertQuery()->data($data);
403-
404-
$this->connection()->sqlExecute(...$query->toSql());
403+
$this->insert($data);
405404

406405
if (!$this->getAutoIncrement() and $column = $this->autoIncrement()) {
407406
$row[$column] = (int) $this->connection()->lastInsertId();
@@ -429,6 +428,7 @@ public function save(array $values = [])
429428

430429
/**
431430
* @return $this
431+
* @throws SqlException
432432
*/
433433
public function destroy()
434434
{

tests/EntityModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testCanFetchEntity()
5353

5454
$model = $this->newEntityModel($connection);
5555

56-
$item = $model->create(['Id' => 1]);
56+
$item = $model->new(['Id' => 1]);
5757

5858
$this->assertInstanceOf(TableEntity::class, $item);
5959

0 commit comments

Comments
 (0)