Skip to content

Commit aa60010

Browse files
committed
fix: Return behavior $_cast in Entity
1 parent 12aa25d commit aa60010

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

system/Entity/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ protected function castAs($value, string $attribute, string $method = 'get')
578578
*/
579579
protected function dataCaster(): ?DataCaster
580580
{
581-
if ($this->casts === [] || ! $this->_cast) {
581+
if ($this->casts === []) {
582582
$this->dataCaster = null;
583583

584584
return null;

tests/system/Entity/EntityTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,14 +1503,14 @@ public function testDataCasterInit(): void
15031503
$this->assertInstanceOf(DataCaster::class, $this->getPrivateProperty($entity, 'dataCaster'));
15041504
$this->assertSame(12345, $entity->first);
15051505

1506-
// Disable casting, do not load DataCaster
1506+
// Disable casting, but the DataCaster is initialized
15071507
$entity->cast(false);
1508-
$this->assertNull($getDataCaster());
1509-
$this->assertNull($this->getPrivateProperty($entity, 'dataCaster'));
1508+
$this->assertInstanceOf(DataCaster::class, $getDataCaster());
1509+
$this->assertInstanceOf(DataCaster::class, $this->getPrivateProperty($entity, 'dataCaster'));
15101510
$this->assertIsString($entity->first);
15111511

1512-
// Method castAs() depends on the $_cast option
1513-
$this->assertSame('12345', $this->getPrivateMethodInvoker($entity, 'castAs')('12345', 'first'));
1512+
// Method castAs() ignore on the $_cast option
1513+
$this->assertSame(12345, $this->getPrivateMethodInvoker($entity, 'castAs')('12345', 'first'));
15141514

15151515
// Restore casting
15161516
$entity->cast(true);
@@ -1519,6 +1519,37 @@ public function testDataCasterInit(): void
15191519
$this->assertSame(12345, $entity->first);
15201520
}
15211521

1522+
public function testDataCasterInitEmptyCasts(): void
1523+
{
1524+
$entity = new class () extends Entity {
1525+
protected $attributes = [
1526+
'first' => '12345',
1527+
];
1528+
protected $casts = [];
1529+
};
1530+
1531+
$getDataCaster = $this->getPrivateMethodInvoker($entity, 'dataCaster');
1532+
1533+
$this->assertNull($getDataCaster());
1534+
$this->assertNull($this->getPrivateProperty($entity, 'dataCaster'));
1535+
$this->assertSame('12345', $entity->first);
1536+
1537+
// Disable casting, the DataCaster was not initialized
1538+
$entity->cast(false);
1539+
$this->assertNull($getDataCaster());
1540+
$this->assertNull($this->getPrivateProperty($entity, 'dataCaster'));
1541+
$this->assertSame('12345', $entity->first);
1542+
1543+
// Method castAs() depends on the $_cast option
1544+
$this->assertSame('12345', $this->getPrivateMethodInvoker($entity, 'castAs')('12345', 'first'));
1545+
1546+
// Restore casting
1547+
$entity->cast(true);
1548+
$this->assertNull($getDataCaster());
1549+
$this->assertNull($this->getPrivateProperty($entity, 'dataCaster'));
1550+
$this->assertSame('12345', $entity->first);
1551+
}
1552+
15221553
private function getEntity(): object
15231554
{
15241555
return new class () extends Entity {

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ your code.
114114
Entity and DataCaster
115115
---------------------
116116

117-
Previously, the ``DataCaster`` object was always initialized, even if you did not use the type casting:
118-
configured ``$_cast = false`` or having an empty array ``$casts = []``.
119-
Now, the object is created on-demand and deleted when type casting is disabled.
117+
Previously, the ``DataCaster`` object was always initialized, even if you did not use the type casting (
118+
configured empty array ``$casts = []``).
119+
Now, the object is created on-demand and it will be ``null`` when type casting is not configured.
120120
In general, the change does not break the existing process, it should be remembered that now in some cases ``$dataCaster`` may be nullable.
121121

122122
Encryption Handlers

user_guide_src/source/models/entities.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ Assume you have a database table named ``users`` that has the following schema::
4040
password - string
4141
created_at - datetime
4242

43-
.. important:: ``attributes`` is a reserved word for internal use. Prior to v4.4.0, if you use it as a column name, the Entity does not work correctly.
44-
4543
Create the Entity Class
4644
=======================
4745

0 commit comments

Comments
 (0)