forked from doctrine/persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reflection] Use default typed property value when setting value to null
Related to doctrine/orm#7999 I'm using typed properties with a default value that is not null. Example: ```php class Entity { /** * @Orm\Column(name="id", type="integer") * @Orm\Id * @Orm\GeneratedValue(strategy="AUTO") */ private int $id = 0; public function getId() : int { return $this->id; } } ``` This works great. When inserting a new entity in the database, the auto incremented id is updated properly. It means that entities that are not persisted will have a `0` as id and the rest will have another non-zero integer value. But upon deleting an entity this becomes a problem. The ORM tries to set the `id` property value to `null` and that causes a TypeError: ``` TypeError: Typed property Entity::$id must be int, null used vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1246 vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:441 vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:376 ``` With this change applied, the default value of the `id` property `0` is used again. Solving the problem.
- Loading branch information
Showing
4 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/Doctrine/Persistence/Reflection/TypedWithDefaultReflectionProperty.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Doctrine\Persistence\Reflection; | ||
|
||
use ReflectionProperty; | ||
|
||
/** | ||
* PHP Typed With Default Reflection Property - special override for typed properties with a default value. | ||
*/ | ||
class TypedWithDefaultReflectionProperty extends ReflectionProperty | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* Works around the problem with setting typed default properties to | ||
* NULL which is not supported, instead assign default value to property. | ||
*/ | ||
public function setValue($object, $value = null) | ||
{ | ||
if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) { | ||
$propertyName = $this->getName(); | ||
$defaultProperties = $this->getDeclaringClass()->getDefaultProperties(); | ||
|
||
$setter = function () use ($propertyName, $defaultProperties): void { | ||
$this->$propertyName = $defaultProperties[$propertyName]; | ||
}; | ||
$setter = $setter->bindTo($object, $this->getDeclaringClass()->getName()); | ||
$setter(); | ||
|
||
return; | ||
} | ||
|
||
parent::setValue($object, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/Doctrine/Tests_PHP74/Persistence/Reflection/TypedWithDefaultReflectionPropertyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests_PHP74\Persistence\Reflection; | ||
|
||
use Doctrine\Persistence\Reflection\TypedWithDefaultReflectionProperty; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TypedWithDefaultReflectionPropertyTest extends TestCase | ||
{ | ||
public function testSetValueNull(): void | ||
{ | ||
$reflection = new TypedWithDefaultReflectionProperty(TypedFooWithDefault::class, 'id'); | ||
$reflection->setAccessible(true); | ||
|
||
$object = new TypedFooWithDefault(); | ||
$object->setId(1); | ||
|
||
self::assertTrue($reflection->isInitialized($object)); | ||
|
||
$reflection->setValue($object, null); | ||
|
||
self::assertSame(0, $reflection->getValue($object)); | ||
self::assertTrue($reflection->isInitialized($object)); | ||
} | ||
} | ||
|
||
class TypedFooWithDefault | ||
{ | ||
private int $id = 0; | ||
|
||
public function setId(int $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
} |