|
3 | 3 |
|
4 | 4 | namespace Articulate\Concise\Support;
|
5 | 5 |
|
6 |
| -use App\Models\User; |
7 | 6 | use Articulate\Concise\Concise;
|
8 | 7 | use Articulate\Concise\Contracts\EntityMapper;
|
9 | 8 | use Articulate\Concise\Contracts\Repository;
|
10 | 9 | use Illuminate\Database\Connection;
|
11 | 10 | use Illuminate\Database\Query\Builder;
|
| 11 | +use Illuminate\Support\Arr; |
12 | 12 | use Illuminate\Support\Collection;
|
13 | 13 |
|
14 | 14 | /**
|
@@ -78,7 +78,7 @@ protected function connection(): Connection
|
78 | 78 | */
|
79 | 79 | protected function query(): Builder
|
80 | 80 | {
|
81 |
| - return $this->connection()->query(); |
| 81 | + return $this->connection()->query()->from($this->mapper()->table()); |
82 | 82 | }
|
83 | 83 |
|
84 | 84 | /**
|
@@ -120,4 +120,35 @@ protected function hydrateMany(Collection $collection): Collection
|
120 | 120 |
|
121 | 121 | return $newCollection;
|
122 | 122 | }
|
| 123 | + |
| 124 | + /** |
| 125 | + * Save the given entity. |
| 126 | + * |
| 127 | + * @param object $entity The entity to be saved. |
| 128 | + * |
| 129 | + * @phpstan-param EntityType $entity |
| 130 | + * |
| 131 | + * @return bool Returns true if the object is successfully saved, false otherwise. |
| 132 | + */ |
| 133 | + public function save(object $entity): bool |
| 134 | + { |
| 135 | + $identity = $this->mapper()->identity($entity); |
| 136 | + $data = $this->mapper()->toData($entity); |
| 137 | + |
| 138 | + if ($identity === null) { |
| 139 | + $id = $this->query()->insertGetId($data); |
| 140 | + |
| 141 | + if (method_exists($entity, 'setId')) { |
| 142 | + $entity->setId($id); |
| 143 | + } else if (property_exists($entity, 'id')) { |
| 144 | + $entity->id = $id; |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + return $this->query() |
| 151 | + ->where('id', $identity) |
| 152 | + ->update(Arr::except($data, ['id'])) > 0; |
| 153 | + } |
123 | 154 | }
|
0 commit comments