Tharos / LeanMapper

Lean Mapper is a tiny ORM based on powerful Dibi database abstraction library for PHP.
MIT License
87 stars 35 forks source link

Property DateTime #160

Closed real-antonin closed 3 years ago

real-antonin commented 3 years ago

Hello,

with strict types, I came across an annoying issue related to DateTime. Maybe it's an intention, but I'll ask anyway.

I have an entity.

<?php declare(strict_types=1);

namespace App\ORM\Entity;

use LeanMapper\Entity;
use Dibi\DateTime;

/**
 * @property int $id
 * @property int $status
 * @property DateTime|null $publish
 */
class MediaEntity extends Entity
{
}

In this case, if I call $medaEntity->publish, I get an exception.

Property 'publish' in entity App\ORM\Entity\MediaEntity is expected to contain an instance of App\ORM\Entity\DateTime, instance of Dibi\DateTime given.

The only way to work around this is to overwrite the publish property to \Dibi\DateTime. I found that the PropertyType class overrides properties according to namespaces, but I would avoid that in this case.

janpecha commented 3 years ago

Hello! Which version of Lean Mapper you are using?

real-antonin commented 3 years ago

Hello! Which version of Lean Mapper you are using?

dev-develop

janpecha commented 3 years ago

It's weird :-/ I tried it on latest dev-develop, but everything works fine. Can you try elaborate in your code where error occured? It can be caused by custom EntityReflection or by class LeanMapper\Reflection\Aliases (or AliasesParser).

real-antonin commented 3 years ago

I do not modify the EntityReflection or Aliases classes.

The method that renames DateTime to App\ORM\Entity\DateTime is translate. https://github.com/Tharos/LeanMapper/blob/f421bad95caab0f14443a6f85d514ff87b2bd9a6/src/LeanMapper/Reflection/PropertyType.php#L45

We create an instance of the PropertyType class here. https://github.com/Tharos/LeanMapper/blob/f421bad95caab0f14443a6f85d514ff87b2bd9a6/src/LeanMapper/Reflection/PropertyFactory.php#L73

It all ends on this line. https://github.com/Tharos/LeanMapper/blob/f421bad95caab0f14443a6f85d514ff87b2bd9a6/src/LeanMapper/Entity.php#L609-L614

janpecha commented 3 years ago

Thanks, fixed :)