Skip to content

Commit b4e4db5

Browse files
authored
Refactoring (#324)
1 parent 107cd30 commit b4e4db5

4 files changed

Lines changed: 26 additions & 35 deletions

File tree

src/ActiveQuery.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
205205
if ($viaCallableUsed) {
206206
$viaModels = $viaQuery->all();
207207
} elseif ($this->primaryModel->isRelationPopulated($viaName)) {
208-
$viaModels = $this->primaryModel->$viaName;
208+
$viaModels = $this->primaryModel->relation($viaName);
209209
} else {
210210
$viaModels = $viaQuery->all();
211211
$this->primaryModel->populateRelation($viaName, $viaModels);
@@ -214,7 +214,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
214214
if ($viaCallableUsed) {
215215
$model = $viaQuery->onePopulate();
216216
} elseif ($this->primaryModel->isRelationPopulated($viaName)) {
217-
$model = $this->primaryModel->$viaName;
217+
$model = $this->primaryModel->relation($viaName);
218218
} else {
219219
$model = $viaQuery->onePopulate();
220220
$this->primaryModel->populateRelation($viaName, $model);
@@ -927,7 +927,7 @@ protected function findByCondition(mixed $condition): static
927927
$condition = [$condition];
928928
}
929929

930-
if (!DbArrayHelper::isAssociative($condition) && !$condition instanceof ExpressionInterface) {
930+
if (!DbArrayHelper::isAssociative($condition)) {
931931
/** query by primary key */
932932
$primaryKey = $arInstance->primaryKey();
933933

src/ActiveRecord.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
* To declare an ActiveRecord class you need to extend {@see ActiveRecord} and implement the `getTableName` method:
5252
*
5353
* ```php
54-
* <?php
55-
*
5654
* class Customer extends ActiveRecord
5755
* {
5856
* public static function getTableName(): string

src/ActiveRecordInterface.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ interface ActiveRecordInterface
1818
*
1919
* The default implementation will return all column names of the table associated with this AR class.
2020
*
21-
* @throws InvalidConfigException
22-
* @throws Exception
23-
*
2421
* @return array List of attribute names.
2522
*
2623
* @psalm-return string[]
@@ -77,7 +74,7 @@ public function deleteAll(array $condition = []): int;
7774
* Returns a value indicating whether the given active record is the same as the current one.
7875
*
7976
* The comparison is made by comparing the table names and the primary key values of the two active records. If one
80-
* of the records {@see isNewRecord|is new} they're also considered not equal.
77+
* of the records {@see getIsNewRecord|is new} they're also considered not equal.
8178
*
8279
* @param self $record Record to compare to.
8380
*
@@ -190,20 +187,19 @@ public function getPrimaryKey(bool $asArray = false): mixed;
190187
/**
191188
* Returns the relation object with the specified name.
192189
*
193-
* @param string $name The relation name (case-sensitive).
190+
* @param string $name The relation name, for example `orders` (case-sensitive).
194191
*
195192
* @return ActiveRecordInterface|array|null The relation object.
196193
*/
197194
public function relation(string $name): self|array|null;
198195

199196
/**
200-
* Returns the relation object with the specified name.
197+
* Returns the relation query object with the specified name.
201198
*
202199
* A relation is defined by a getter method which returns an object implementing the {@see ActiveQueryInterface}
203200
* (normally this would be a relational {@see ActiveQuery} object).
204201
*
205-
* @param string $name The relation name, for example `orders` for a relation defined via `getOrders()` method
206-
* (case-sensitive).
202+
* @param string $name The relation name, for example `orders` (case-sensitive).
207203
* @param bool $throwException Whether to throw exception if the relation doesn't exist.
208204
*
209205
* @return ActiveQueryInterface|null The relational query object.
@@ -305,17 +301,6 @@ public function isRelationPopulated(string $name): bool;
305301
*/
306302
public function link(string $name, self $arClass, array $extraColumns = []): void;
307303

308-
/**
309-
* Returns the element at the specified offset.
310-
*
311-
* This method is required by the SPL interface {@see ArrayAccess}.
312-
*
313-
* It's implicitly called when you use something like `$value = $model[$offset];`.
314-
*
315-
* @return mixed the element at the offset, null if no element is found at the offset
316-
*/
317-
public function offsetGet(mixed $offset): mixed;
318-
319304
/**
320305
* Populates the named relation with the related records.
321306
*

src/ActiveRelationTrait.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public function __clone()
8686
* ```
8787
*
8888
* @param string $relationName the relation name. This refers to a relation declared in {@see primaryModel}.
89-
* @param callable|null $callable $callable a PHP callback for customizing the relation associated with the junction
90-
* table.
89+
* @param callable|null $callable a PHP callback for customizing the relation associated with the junction table.
9190
* Its signature should be `function($query)`, where `$query` is the query to be customized.
9291
*
9392
* @return static the relation object itself.
@@ -98,7 +97,7 @@ public function via(string $relationName, callable $callable = null): static
9897
$callableUsed = $callable !== null;
9998
$this->via = [$relationName, $relation, $callableUsed];
10099

101-
if ($callable !== null) {
100+
if ($callableUsed) {
102101
$callable($relation);
103102
}
104103

@@ -164,13 +163,10 @@ public function inverseOf(string $relationName): static
164163
}
165164

166165
/**
167-
* Finds the related records for the specified primary record.
166+
* Returns query records depends on {@see $multiple} .
168167
*
169168
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
170169
*
171-
* @param string $name the relation name.
172-
* @param ActiveRecordInterface $model the primary model.
173-
*
174170
* @throws Exception
175171
* @throws InvalidArgumentException
176172
* @throws InvalidConfigException
@@ -612,9 +608,19 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
612608
{
613609
$key = [];
614610

615-
foreach ($attributes as $attribute) {
616-
if (isset($activeRecord[$attribute]) || (is_object($activeRecord) && property_exists($activeRecord, $attribute))) {
617-
$key[] = $this->normalizeModelKey($activeRecord[$attribute]);
611+
if (is_array($activeRecord)) {
612+
foreach ($attributes as $attribute) {
613+
if (isset($activeRecord[$attribute])) {
614+
$key[] = $this->normalizeModelKey($activeRecord[$attribute]);
615+
}
616+
}
617+
} else {
618+
foreach ($attributes as $attribute) {
619+
$value = $activeRecord->getAttribute($attribute);
620+
621+
if ($value !== null) {
622+
$key[] = $this->normalizeModelKey($value);
623+
}
618624
}
619625
}
620626

@@ -628,9 +634,11 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
628634
}
629635

630636
/**
637+
* @param int|string|Stringable|null $value raw key value.
638+
*
631639
* @return int|string|null normalized key value.
632640
*/
633-
private function normalizeModelKey(mixed $value): int|string|null
641+
private function normalizeModelKey(int|string|Stringable|null $value): int|string|null
634642
{
635643
if ($value instanceof Stringable) {
636644
/**

0 commit comments

Comments
 (0)