Skip to content

Commit 9d3337f

Browse files
authored
Merge branch 'master' into fix-94
2 parents 830c0c5 + 618522c commit 9d3337f

File tree

9 files changed

+77
-52
lines changed

9 files changed

+77
-52
lines changed

docs/create-model.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,19 @@ Notes:
152152

153153
### Private properties
154154

155-
To use `private` properties inside the model class, you need to copy `propertyValuesInternal()` and `populateProperty()`
156-
methods from the `ActiveRecord` class and adjust them to work with the `private` properties.
155+
To use `private` properties inside the model class, you need to use the `PrivatePropertiesTrait` trait.
157156

158157
```php
159158
use Yiisoft\ActiveRecord\ActiveRecord;
159+
use Yiisoft\ActiveRecord\Trait\PrivatePropertiesTrait;
160160

161161
/**
162162
* Entity User.
163163
**/
164164
final class User extends ActiveRecord
165165
{
166+
use PrivatePropertiesTrait;
167+
166168
private int $id;
167169
private string $username;
168170
private string $email;
@@ -175,17 +177,6 @@ final class User extends ActiveRecord
175177

176178
// Getters and setters as for protected properties
177179
// ...
178-
179-
// Copied `propertyValuesInternal()` and `populateProperty()` methods from `ActiveRecord` class
180-
protected function propertyValuesInternal(): array
181-
{
182-
return get_object_vars($this);
183-
}
184-
185-
protected function populateProperty(string $name, mixed $value): void
186-
{
187-
$this->$name = $value;
188-
}
189180
}
190181
```
191182

src/.meta-storm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<classMethod class="\Yiisoft\ActiveRecord\ActiveRecordInterface" method="hasProperty" argument="0">
1111
<properties relatedTo="variable" />
1212
</classMethod>
13-
<classMethod class="\Yiisoft\ActiveRecord\ActiveRecordInterface" method="columnType" argument="0">
13+
<classMethod class="\Yiisoft\ActiveRecord\ActiveRecordInterface" method="column" argument="0">
1414
<properties relatedTo="variable" />
1515
</classMethod>
1616
</definitions>

src/ActiveRecord.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yiisoft\Db\Exception\Exception;
99
use Yiisoft\Db\Exception\InvalidCallException;
1010
use Yiisoft\Db\Exception\InvalidConfigException;
11+
use Yiisoft\Db\Schema\Column\ColumnInterface;
1112
use Yiisoft\Db\Schema\TableSchemaInterface;
1213

1314
use function array_diff_key;
@@ -84,9 +85,10 @@ public function propertyNames(): array
8485
return $this->tableSchema()->getColumnNames();
8586
}
8687

87-
public function columnType(string $propertyName): string
88+
public function column(string $propertyName): ColumnInterface
8889
{
89-
return $this->tableSchema()->getColumn($propertyName)?->getType() ?? ColumnType::STRING;
90+
return $this->tableSchema()->getColumn($propertyName)
91+
?? $this->db()->getColumnFactory()->fromType(ColumnType::STRING, ['name' => $propertyName]);
9092
}
9193

9294
/**

src/ActiveRecordInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use Throwable;
88
use Yiisoft\Db\Connection\ConnectionInterface;
9-
use Yiisoft\Db\Constant\ColumnType;
109
use Yiisoft\Db\Exception\Exception;
1110
use InvalidArgumentException;
1211
use Yiisoft\Db\Exception\InvalidCallException;
1312
use Yiisoft\Db\Exception\InvalidConfigException;
13+
use Yiisoft\Db\Schema\Column\ColumnInterface;
1414

1515
/**
1616
* @psalm-import-type ModelClass from ActiveQuery
@@ -27,11 +27,9 @@ interface ActiveRecordInterface
2727
public function propertyNames(): array;
2828

2929
/**
30-
* Returns the abstract type of the property.
31-
*
32-
* @psalm-return ColumnType::*
30+
* @return ColumnInterface The column instance of the property.
3331
*/
34-
public function columnType(string $propertyName): string;
32+
public function column(string $propertyName): ColumnInterface;
3533

3634
/**
3735
* Create {@see ActiveQueryInterface} instance for the given model class.

src/ActiveRelationTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Yiisoft\Db\Exception\Exception;
1212
use InvalidArgumentException;
1313
use Yiisoft\Db\Exception\InvalidConfigException;
14+
use Yiisoft\Db\Expression\Value\ArrayValue;
1415
use Yiisoft\Db\QueryBuilder\Condition\In;
1516
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps;
1617
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlaps;
@@ -569,9 +570,10 @@ protected function filterByModels(array $models): void
569570
$columnName = reset($columnNames);
570571
/** @var string $propertyName */
571572
$propertyName = array_key_first($this->link);
573+
$column = $this->getModel()->column($propertyName);
572574

573-
match ($this->getModel()->columnType($propertyName)) {
574-
ColumnType::ARRAY => $this->andWhere(new ArrayOverlaps($columnName, $values)),
575+
match ($column->getType()) {
576+
ColumnType::ARRAY => $this->andWhere(new ArrayOverlaps($columnName, new ArrayValue($values, $column))),
575577
ColumnType::JSON => $this->andWhere(new JsonOverlaps($columnName, $values)),
576578
default => $this->andWhere(new In($columnName, $values)),
577579
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\ActiveRecord\Trait;
6+
7+
use Yiisoft\ActiveRecord\AbstractActiveRecord;
8+
9+
use function get_object_vars;
10+
11+
/**
12+
* Trait to handle private properties in Active Record classes.
13+
*
14+
* The trait required when using private properties inside the model class.
15+
*
16+
* @link https://github.com/yiisoft/active-record/blob/master/docs/create-model.md#private-properties
17+
*
18+
* @see AbstractActiveRecord::propertyValuesInternal()
19+
* @see AbstractActiveRecord::populateProperty()
20+
*/
21+
trait PrivatePropertiesTrait
22+
{
23+
protected function propertyValuesInternal(): array
24+
{
25+
return get_object_vars($this);
26+
}
27+
28+
protected function populateProperty(string $name, mixed $value): void
29+
{
30+
$this->$name = $value;
31+
}
32+
}

tests/Driver/Pgsql/ActiveRecordTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR;
1717
use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper;
1818
use Yiisoft\Db\Connection\ConnectionInterface;
19-
use Yiisoft\Db\Expression\ArrayExpression;
19+
use Yiisoft\Db\Expression\Value\ArrayValue;
2020
use Yiisoft\Db\Expression\Expression;
21-
use Yiisoft\Db\Expression\JsonExpression;
21+
use Yiisoft\Db\Expression\Value\JsonValue;
2222
use Yiisoft\Db\Pgsql\Schema as SchemaPgsql;
2323
use Yiisoft\Factory\Factory;
2424

@@ -234,16 +234,16 @@ public static function arrayValuesProvider(): array
234234
return [
235235
'simple arrays values' => [[
236236
'intarray_col' => [
237-
new ArrayExpression([1,-2,null,'42'], 'int4'),
237+
new ArrayValue([1,-2,null,'42'], 'int4'),
238238
[1,-2,null,42],
239239
],
240240
'textarray2_col' => [
241-
new ArrayExpression([['text'], [null], [1]], 'text[][]'),
241+
new ArrayValue([['text'], [null], [1]], 'text[][]'),
242242
[['text'], [null], ['1']],
243243
],
244244
'json_col' => [['a' => 1, 'b' => null, 'c' => [1,3,5]]],
245245
'jsonb_col' => [[null, 'a', 'b', '\"', '{"af"}']],
246-
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], 'json')],
246+
'jsonarray_col' => [new ArrayValue([[',', 'null', true, 'false', 'f']], 'json')],
247247
]],
248248
'null arrays values' => [[
249249
'intarray_col' => [
@@ -268,19 +268,19 @@ public static function arrayValuesProvider(): array
268268
]],
269269
'arrays packed in classes' => [[
270270
'intarray_col' => [
271-
new ArrayExpression([1,-2,null,'42'], 'int[]'),
271+
new ArrayValue([1,-2,null,'42'], 'int[]'),
272272
[1,-2,null,42],
273273
],
274274
'textarray2_col' => [
275-
new ArrayExpression([['text'], [null], [1]], 'text[][]'),
275+
new ArrayValue([['text'], [null], [1]], 'text[][]'),
276276
[['text'], [null], ['1']],
277277
],
278278
'json_col' => [
279-
new JsonExpression(['a' => 1, 'b' => null, 'c' => [1,3,5]]),
279+
new JsonValue(['a' => 1, 'b' => null, 'c' => [1,3,5]]),
280280
['a' => 1, 'b' => null, 'c' => [1,3,5]],
281281
],
282282
'jsonb_col' => [
283-
new JsonExpression([null, 'a', 'b', '\"', '{"af"}']),
283+
new JsonValue([null, 'a', 'b', '\"', '{"af"}']),
284284
[null, 'a', 'b', '\"', '{"af"}'],
285285
],
286286
'jsonarray_col' => [
@@ -322,8 +322,8 @@ public function testArrayValues($properties): void
322322
$expected = $expected[1] ?? $expected[0];
323323
$value = $type->get($property);
324324

325-
if ($expected instanceof ArrayExpression) {
326-
$expected = $expected->getValue();
325+
if ($expected instanceof ArrayValue) {
326+
$expected = $expected->value;
327327
}
328328

329329
$this->assertSame($expected, $value, 'In column ' . $property);

tests/Driver/Pgsql/MagicActiveRecordTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\UserAR;
1313
use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper;
1414
use Yiisoft\Db\Connection\ConnectionInterface;
15-
use Yiisoft\Db\Expression\ArrayExpression;
15+
use Yiisoft\Db\Expression\Value\ArrayValue;
1616
use Yiisoft\Db\Expression\Expression;
17-
use Yiisoft\Db\Expression\JsonExpression;
17+
use Yiisoft\Db\Expression\Value\JsonValue;
1818
use Yiisoft\Db\Pgsql\Schema as SchemaPgsql;
1919

2020
final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest
@@ -185,16 +185,16 @@ public static function arrayValuesProvider(): array
185185
return [
186186
'simple arrays values' => [[
187187
'intarray_col' => [
188-
new ArrayExpression([1,-2,null,'42'], 'int4'),
188+
new ArrayValue([1,-2,null,'42'], 'int4'),
189189
[1,-2,null,42],
190190
],
191191
'textarray2_col' => [
192-
new ArrayExpression([['text'], [null], [1]], 'text[][]'),
192+
new ArrayValue([['text'], [null], [1]], 'text[][]'),
193193
[['text'], [null], ['1']],
194194
],
195195
'json_col' => [['a' => 1, 'b' => null, 'c' => [1,3,5]]],
196196
'jsonb_col' => [[null, 'a', 'b', '\"', '{"af"}']],
197-
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], 'json')],
197+
'jsonarray_col' => [new ArrayValue([[',', 'null', true, 'false', 'f']], 'json')],
198198
]],
199199
'null arrays values' => [[
200200
'intarray_col' => [
@@ -219,19 +219,19 @@ public static function arrayValuesProvider(): array
219219
]],
220220
'arrays packed in classes' => [[
221221
'intarray_col' => [
222-
new ArrayExpression([1,-2,null,'42'], 'int'),
222+
new ArrayValue([1,-2,null,'42'], 'int'),
223223
[1,-2,null,42],
224224
],
225225
'textarray2_col' => [
226-
new ArrayExpression([['text'], [null], [1]], 'text[][]'),
226+
new ArrayValue([['text'], [null], [1]], 'text[][]'),
227227
[['text'], [null], ['1']],
228228
],
229229
'json_col' => [
230-
new JsonExpression(['a' => 1, 'b' => null, 'c' => [1,3,5]]),
230+
new JsonValue(['a' => 1, 'b' => null, 'c' => [1,3,5]]),
231231
['a' => 1, 'b' => null, 'c' => [1,3,5]],
232232
],
233233
'jsonb_col' => [
234-
new JsonExpression([null, 'a', 'b', '\"', '{"af"}']),
234+
new JsonValue([null, 'a', 'b', '\"', '{"af"}']),
235235
[null, 'a', 'b', '\"', '{"af"}'],
236236
],
237237
'jsonarray_col' => [
@@ -273,8 +273,8 @@ public function testArrayValues($properties): void
273273
$expected = $expected[1] ?? $expected[0];
274274
$value = $type->$property;
275275

276-
if ($expected instanceof ArrayExpression) {
277-
$expected = $expected->getValue();
276+
if ($expected instanceof ArrayValue) {
277+
$expected = $expected->value;
278278
}
279279

280280
$this->assertSame($expected, $value, 'In column ' . $property);

tests/Stubs/ActiveRecord/ArrayAndJsonTypes.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
66

77
use Yiisoft\ActiveRecord\ActiveRecord;
8-
use Yiisoft\Db\Expression\ArrayExpression;
8+
use Yiisoft\Db\Expression\Value\ArrayValue;
99
use Yiisoft\Db\Expression\Expression;
10-
use Yiisoft\Db\Expression\JsonExpression;
10+
use Yiisoft\Db\Expression\Value\JsonValue;
1111

1212
final class ArrayAndJsonTypes extends ActiveRecord
1313
{
1414
public int $id;
15-
public array|ArrayExpression|null $intarray_col = null;
16-
public array|ArrayExpression|null $textarray2_col = null;
17-
public array|float|int|string|JsonExpression|null $json_col = null;
18-
public array|float|int|string|JsonExpression|null $jsonb_col = null;
19-
public array|ArrayExpression|Expression|null $jsonarray_col = null;
15+
public array|ArrayValue|null $intarray_col = null;
16+
public array|ArrayValue|null $textarray2_col = null;
17+
public array|float|int|string|JsonValue|null $json_col = null;
18+
public array|float|int|string|JsonValue|null $jsonb_col = null;
19+
public array|ArrayValue|Expression|null $jsonarray_col = null;
2020
}

0 commit comments

Comments
 (0)