Skip to content

Commit 144bee6

Browse files
committed
Avoid set and get prefixes for AR methods
1 parent 4958cfa commit 144bee6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+208
-208
lines changed

docs/create-model.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use Yiisoft\ActiveRecord\ActiveRecord;
2626
#[\AllowDynamicProperties]
2727
final class User extends ActiveRecord
2828
{
29-
public function getTableName(): string
29+
public function tableName(): string
3030
{
3131
return '{{%user}}';
3232
}
@@ -66,7 +66,7 @@ final class User extends ActiveRecord
6666
public string $email;
6767
public string $status = 'active';
6868

69-
public function getTableName(): string
69+
public function tableName(): string
7070
{
7171
return '{{%user}}';
7272
}
@@ -96,7 +96,7 @@ final class User extends ActiveRecord
9696
protected string $email;
9797
protected string $status = 'active';
9898

99-
public function getTableName(): string
99+
public function tableName(): string
100100
{
101101
return '{{%user}}';
102102
}
@@ -168,7 +168,7 @@ final class User extends ActiveRecord
168168
private string $email;
169169
private string $status = 'active';
170170

171-
public function getTableName(): string
171+
public function tableName(): string
172172
{
173173
return '{{%user}}';
174174
}
@@ -213,7 +213,7 @@ final class User extends ActiveRecord
213213
{
214214
use MagicPropertiesTrait;
215215

216-
public function getTableName(): string
216+
public function tableName(): string
217217
{
218218
return '{{%user}}';
219219
}

src/AbstractActiveRecord.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ public function delete(): int
9595
public function deleteAll(array $condition = [], array $params = []): int
9696
{
9797
$command = $this->db()->createCommand();
98-
$command->delete($this->getTableName(), $condition, $params);
98+
$command->delete($this->tableName(), $condition, $params);
9999

100100
return $command->execute();
101101
}
102102

103103
public function equals(ActiveRecordInterface $record): bool
104104
{
105-
if ($this->getIsNewRecord() || $record->getIsNewRecord()) {
105+
if ($this->isNewRecord() || $record->isNewRecord()) {
106106
return false;
107107
}
108108

109-
return $this->getTableName() === $record->getTableName() && $this->getPrimaryKeys() === $record->getPrimaryKeys();
109+
return $this->tableName() === $record->tableName() && $this->primaryKeyValues() === $record->primaryKeyValues();
110110
}
111111

112112
public function get(string $propertyName): mixed
@@ -125,7 +125,7 @@ public function propertyValues(array|null $names = null, array $except = []): ar
125125
return array_intersect_key($this->propertyValuesInternal(), array_flip($names));
126126
}
127127

128-
public function getIsNewRecord(): bool
128+
public function isNewRecord(): bool
129129
{
130130
return $this->oldValues === null;
131131
}
@@ -180,30 +180,30 @@ public function oldValues(): array
180180
return $this->oldValues ?? [];
181181
}
182182

183-
public function getOldPrimaryKey(): float|int|string|null
183+
public function primaryKeyOldValue(): float|int|string|null
184184
{
185185
$keys = $this->primaryKey();
186186

187187
return match (count($keys)) {
188188
1 => $this->oldValues[$keys[0]] ?? null,
189189
0 => throw new Exception(
190190
static::class . ' does not have a primary key. You should either define a primary key for '
191-
. $this->getTableName() . ' table or override the primaryKey() method.'
191+
. $this->tableName() . ' table or override the primaryKey() method.'
192192
),
193193
default => throw new Exception(
194-
static::class . ' has multiple primary keys. Use getOldPrimaryKeys() method instead.'
194+
static::class . ' has multiple primary keys. Use primaryKeyOldValues() method instead.'
195195
),
196196
};
197197
}
198198

199-
public function getOldPrimaryKeys(): array
199+
public function primaryKeyOldValues(): array
200200
{
201201
$keys = $this->primaryKey();
202202

203203
if (empty($keys)) {
204204
throw new Exception(
205205
static::class . ' does not have a primary key. You should either define a primary key for '
206-
. $this->getTableName() . ' table or override the primaryKey() method.'
206+
. $this->tableName() . ' table or override the primaryKey() method.'
207207
);
208208
}
209209

@@ -216,30 +216,30 @@ public function getOldPrimaryKeys(): array
216216
return $values;
217217
}
218218

219-
public function getPrimaryKey(): float|int|string|null
219+
public function primaryKeyValue(): float|int|string|null
220220
{
221221
$keys = $this->primaryKey();
222222

223223
return match (count($keys)) {
224224
1 => $this->get($keys[0]),
225225
0 => throw new Exception(
226226
static::class . ' does not have a primary key. You should either define a primary key for '
227-
. $this->getTableName() . ' table or override the primaryKey() method.'
227+
. $this->tableName() . ' table or override the primaryKey() method.'
228228
),
229229
default => throw new Exception(
230-
static::class . ' has multiple primary keys. Use getPrimaryKeys() method instead.'
230+
static::class . ' has multiple primary keys. Use primaryKeyValues() method instead.'
231231
),
232232
};
233233
}
234234

235-
public function getPrimaryKeys(): array
235+
public function primaryKeyValues(): array
236236
{
237237
$keys = $this->primaryKey();
238238

239239
if (empty($keys)) {
240240
throw new Exception(
241241
static::class . ' does not have a primary key. You should either define a primary key for '
242-
. $this->getTableName() . ' table or override the primaryKey() method.'
242+
. $this->tableName() . ' table or override the primaryKey() method.'
243243
);
244244
}
245245

@@ -259,7 +259,7 @@ public function getPrimaryKeys(): array
259259
*
260260
* {@see relationQuery()}
261261
*/
262-
public function getRelatedRecords(): array
262+
public function relatedRecords(): array
263263
{
264264
return $this->related;
265265
}
@@ -412,7 +412,7 @@ public function link(string $relationName, ActiveRecordInterface $arClass, array
412412
$via = $relation->getVia();
413413

414414
if ($via !== null) {
415-
if ($this->getIsNewRecord() || $arClass->getIsNewRecord()) {
415+
if ($this->isNewRecord() || $arClass->isNewRecord()) {
416416
throw new InvalidCallException(
417417
'Unable to link models: the models being linked cannot be newly created.'
418418
);
@@ -484,11 +484,11 @@ public function link(string $relationName, ActiveRecordInterface $arClass, array
484484
$p2 = $this->isPrimaryKey(array_values($link));
485485

486486
if ($p1 && $p2) {
487-
if ($this->getIsNewRecord() && $arClass->getIsNewRecord()) {
487+
if ($this->isNewRecord() && $arClass->isNewRecord()) {
488488
throw new InvalidCallException('Unable to link models: at most one model can be newly created.');
489489
}
490490

491-
if ($this->getIsNewRecord()) {
491+
if ($this->isNewRecord()) {
492492
$this->bindModels(array_flip($link), $this, $arClass);
493493
} else {
494494
$this->bindModels($link, $arClass, $this);
@@ -581,7 +581,7 @@ public function populateRelation(string $name, array|ActiveRecordInterface|null
581581
*/
582582
public function refresh(): bool
583583
{
584-
$record = $this->query()->findByPk($this->getOldPrimaryKeys());
584+
$record = $this->query()->findByPk($this->primaryKeyOldValues());
585585

586586
return $this->refreshInternal($record);
587587
}
@@ -621,7 +621,7 @@ protected function retrieveRelation(string $name): ActiveRecordInterface|array|n
621621

622622
public function save(array|null $propertyNames = null): bool
623623
{
624-
if ($this->getIsNewRecord()) {
624+
if ($this->isNewRecord()) {
625625
return $this->insert($propertyNames);
626626
}
627627

@@ -660,15 +660,15 @@ public function populateProperties(array $values): void
660660
}
661661

662662
/**
663-
* Sets the value indicating whether the record is new.
663+
* Marks this record as new.
664664
*
665-
* @param bool $value Whether the record is new and should be inserted when calling {@see save()}.
665+
* @param bool $isNewRecord Whether the record is new and should be inserted when calling {@see save()}.
666666
*
667-
* @see getIsNewRecord()
667+
* @see isNewRecord()
668668
*/
669-
public function setIsNewRecord(bool $value): void
669+
public function markAsNewRecord(bool $isNewRecord = true): void
670670
{
671-
$this->oldValues = $value ? null : $this->propertyValuesInternal();
671+
$this->oldValues = $isNewRecord ? null : $this->propertyValuesInternal();
672672
}
673673

674674
/**
@@ -711,7 +711,7 @@ public function updateAll(array $propertyValues, array|string $condition = [], a
711711
{
712712
$command = $this->db()->createCommand();
713713

714-
$command->update($this->getTableName(), $propertyValues, $condition, $params);
714+
$command->update($this->tableName(), $propertyValues, $condition, $params);
715715

716716
return $command->execute();
717717
}
@@ -753,7 +753,7 @@ public function updateAllCounters(array $counters, array|string $condition = '',
753753
}
754754

755755
$command = $this->db()->createCommand();
756-
$command->update($this->getTableName(), $counters, $condition, $params);
756+
$command->update($this->tableName(), $counters, $condition, $params);
757757

758758
return $command->execute();
759759
}
@@ -785,7 +785,7 @@ public function updateAllCounters(array $counters, array|string $condition = '',
785785
*/
786786
public function updateCounters(array $counters): bool
787787
{
788-
if ($this->updateAllCounters($counters, $this->getOldPrimaryKeys()) === 0) {
788+
if ($this->updateAllCounters($counters, $this->primaryKeyOldValues()) === 0) {
789789
return false;
790790
}
791791

@@ -891,7 +891,7 @@ public function unlink(string $relationName, ActiveRecordInterface $arClass, boo
891891
/** @psalm-var array<array-key, ActiveRecordInterface> $related */
892892
$related = $this->related[$relationName];
893893
foreach ($related as $a => $b) {
894-
if ($arClass->getPrimaryKeys() === $b->getPrimaryKeys()) {
894+
if ($arClass->primaryKeyValues() === $b->primaryKeyValues()) {
895895
unset($this->related[$relationName][$a]);
896896
}
897897
}
@@ -1071,7 +1071,7 @@ protected function deleteInternal(): int
10711071
* We don't check the return value of deleteAll() because it is possible the record is already deleted in
10721072
* the database and thus the method will return 0
10731073
*/
1074-
$condition = $this->getOldPrimaryKeys();
1074+
$condition = $this->primaryKeyOldValues();
10751075

10761076
if ($this instanceof OptimisticLockInterface) {
10771077
$lock = $this->optimisticLockPropertyName();
@@ -1132,7 +1132,7 @@ protected function refreshInternal(array|ActiveRecordInterface|null $record = nu
11321132
*/
11331133
protected function updateInternal(array|null $properties = null): int
11341134
{
1135-
if ($this->getIsNewRecord()) {
1135+
if ($this->isNewRecord()) {
11361136
throw new InvalidCallException('The record is new and cannot be updated.');
11371137
}
11381138

@@ -1157,7 +1157,7 @@ protected function updateInternal(array|null $properties = null): int
11571157
return 0;
11581158
}
11591159

1160-
$condition = $this->getOldPrimaryKeys();
1160+
$condition = $this->primaryKeyOldValues();
11611161

11621162
if ($this instanceof OptimisticLockInterface) {
11631163
$lock = $this->optimisticLockPropertyName();
@@ -1234,7 +1234,7 @@ protected function resetDependentRelations(string $propertyName): void
12341234
unset($this->relationsDependencies[$propertyName]);
12351235
}
12361236

1237-
public function getTableName(): string
1237+
public function tableName(): string
12381238
{
12391239
$name = (new ReflectionClass($this))->getShortName();
12401240
/** @var string $name */

src/ActiveQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public function getTablesUsedInFrom(): array
779779

780780
protected function getPrimaryTableName(): string
781781
{
782-
return $this->getARInstance()->getTableName();
782+
return $this->getARInstance()->tableName();
783783
}
784784

785785
public function getOn(): array|string|null
@@ -823,7 +823,7 @@ public function findByPk(array|float|int|string $values): array|ActiveRecordInte
823823
}
824824

825825
if (!empty($this->getJoins()) || !empty($this->getJoinWith())) {
826-
$tableName = $arInstance->getTableName();
826+
$tableName = $arInstance->tableName();
827827

828828
foreach ($primaryKey as &$pk) {
829829
$pk = "$tableName.$pk";

0 commit comments

Comments
 (0)