Skip to content

Commit 48949ba

Browse files
committed
dedup load across persistences
1 parent 5239f3a commit 48949ba

File tree

6 files changed

+58
-54
lines changed

6 files changed

+58
-54
lines changed

phpstan.neon.dist

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ parameters:
2323
# for src/FieldSqlExpression.php
2424
- '~^Call to an undefined method Atk4\\Data\\Model\:\:expr\(\)\.$~'
2525
# for src/Model.php
26-
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:load\(\)\.$~'
27-
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:tryLoad\(\)\.$~'
28-
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:loadAny\(\)\.$~'
29-
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:tryLoadAny\(\)\.$~'
3026
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:update\(\)\.$~'
3127
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:insert\(\)\.$~'
3228
- '~^Call to an undefined method Atk4\\Data\\Persistence\:\:export\(\)\.$~'

src/Persistence.php

+52
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Persistence
2020
/** @const string */
2121
public const HOOK_AFTER_ADD = self::class . '@afterAdd';
2222

23+
/** @const string */
24+
protected const ID_LOAD_ONE = self::class . '@idLoadOne';
25+
/** @const string */
26+
protected const ID_LOAD_ANY = self::class . '@idLoadAny';
27+
2328
/**
2429
* Connects database.
2530
*
@@ -113,6 +118,53 @@ public function getDatabasePlatform(): Platforms\AbstractPlatform
113118
return new Persistence\GenericPlatform();
114119
}
115120

121+
/**
122+
* Tries to load data record, but will not fail if record can't be loaded.
123+
*
124+
* @param mixed $id
125+
*/
126+
public function tryLoad(Model $model, $id): ?array
127+
{
128+
throw new Exception('Not implemented'); // finish in https://github.com/atk4/data/pull/847
129+
}
130+
131+
/**
132+
* Loads a record from model and returns a associative array.
133+
*
134+
* @param mixed $id
135+
*/
136+
public function load(Model $model, $id): array
137+
{
138+
$data = $this->tryLoad($model, $id);
139+
140+
if (!$data) {
141+
$noId = $id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY;
142+
143+
throw (new Exception('No record was found', 404))
144+
->addMoreInfo('model', $model)
145+
->addMoreInfo('id', $noId ? null : $id)
146+
->addMoreInfo('scope', $model->scope()->toWords());
147+
}
148+
149+
return $data;
150+
}
151+
152+
/**
153+
* Tries to load any one record.
154+
*/
155+
public function tryLoadAny(Model $model): ?array
156+
{
157+
return $this->tryLoad($model, self::ID_LOAD_ANY);
158+
}
159+
160+
/**
161+
* Loads any one record.
162+
*/
163+
public function loadAny(Model $model): array
164+
{
165+
return $this->load($model, self::ID_LOAD_ANY);
166+
}
167+
116168
/**
117169
* Will convert one row of data from native PHP types into
118170
* persistence types. This will also take care of the "actual"

src/Persistence/Array_.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function load(Model $model, $id, string $table = null): array
161161
/**
162162
* Tries to load first available record and return data record.
163163
*/
164-
public function loadAny(Model $model, string $table = null): ?array
164+
public function loadAny(Model $model, string $table = null): array
165165
{
166166
$row = $this->tryLoadAny($model, $table);
167167
if ($row === null) {

src/Persistence/Array_/Join.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use Atk4\Data\Exception;
88
use Atk4\Data\Model;
9+
use Atk4\Data\Persistence;
910

1011
/**
1112
* Provides model joining functionality specific for the Array_ persistence.
13+
*
14+
* @property Persistence\Array_|null $persistence
1215
*/
1316
class Join extends Model\Join
1417
{
@@ -51,7 +54,8 @@ public function afterLoad(): void
5154
}
5255

5356
try {
54-
$data = $model->persistence->load($model, $this->id, $this->foreign_table);
57+
$data = Persistence\Array_::assertInstanceOf($model->persistence)
58+
->load($model, $this->id, $this->foreign_table);
5559
} catch (Exception $e) {
5660
throw (new Exception('Unable to load joined record', $e->getCode(), $e))
5761
->addMoreInfo('table', $this->foreign_table)

src/Persistence/Sql.php

-47
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class Sql extends Persistence
3030
/** @const string */
3131
public const HOOK_BEFORE_DELETE_QUERY = self::class . '@beforeDeleteQuery';
3232

33-
/** @const string */
34-
protected const ID_LOAD_ONE = self::class . '@idLoadOne';
35-
/** @const string */
36-
protected const ID_LOAD_ANY = self::class . '@idLoadAny';
37-
3833
/**
3934
* Connection object.
4035
*
@@ -667,11 +662,6 @@ public function action(Model $model, string $type, array $args = [])
667662
return $query;
668663
}
669664

670-
/**
671-
* Tries to load data record, but will not fail if record can't be loaded.
672-
*
673-
* @param mixed $id
674-
*/
675665
public function tryLoad(Model $model, $id): ?array
676666
{
677667
$noId = $id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY;
@@ -718,43 +708,6 @@ public function tryLoad(Model $model, $id): ?array
718708
return $data;
719709
}
720710

721-
/**
722-
* Loads a record from model and returns a associative array.
723-
*
724-
* @param mixed $id
725-
*/
726-
public function load(Model $model, $id): array
727-
{
728-
$data = $this->tryLoad($model, $id);
729-
730-
if (!$data) {
731-
$noId = $id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY;
732-
733-
throw (new Exception('No record was found', 404))
734-
->addMoreInfo('model', $model)
735-
->addMoreInfo('id', $noId ? null : $id)
736-
->addMoreInfo('scope', $model->scope()->toWords());
737-
}
738-
739-
return $data;
740-
}
741-
742-
/**
743-
* Tries to load any one record.
744-
*/
745-
public function tryLoadAny(Model $model): ?array
746-
{
747-
return $this->tryLoad($model, self::ID_LOAD_ANY);
748-
}
749-
750-
/**
751-
* Loads any one record.
752-
*/
753-
public function loadAny(Model $model): array
754-
{
755-
return $this->load($model, self::ID_LOAD_ANY);
756-
}
757-
758711
/**
759712
* Inserts record in database and returns new record ID.
760713
*/

src/Persistence/Sql/Join.php

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* Provides model joining functionality specific for the Sql persistence.
1414
*
1515
* @property Persistence\Sql $persistence
16-
* @property static $join
1716
*/
1817
class Join extends Model\Join implements \Atk4\Dsql\Expressionable
1918
{

0 commit comments

Comments
 (0)