orm icon indicating copy to clipboard operation
orm copied to clipboard

Entity inheritance

Open iamsaint opened this issue 5 years ago • 1 comments

My classes definition:

** @Entity */
class User
{
    /** @Column(type="primary") */
    public $id;
}

/** @Entity */
class CustomUser extends User
{
    /** @Column(type="string") */
    public $email;
}

DI container configuration

$container = new Container();
$container->setDefinitions([ User::class => CustomUser::class ]);

Finding entity with class defined in DI container

$definitions = $container->getDefinitions();
$definition = $definitions[User::class];
// $definition is equal ['class' => 'path\to\CustomUser']

$entity = $orm->getRepository($definition['class'])->findOne([...]);

Now I try to get class of $entity

get_class($entity);

I expecting to get CustomUser but got User

iamsaint avatar Feb 19 '20 09:02 iamsaint

Hi,

I'm not quite sure what container you use. But both User and CustomUser will share the same repository (we might add a feature to use custom repositories in the future).

Since you store both objects in one table it is possible to receive User instead of CustomUser (unless adding __type condition).

https://cycle-orm.dev/docs/advanced-single-table-inheritance#querying

wolfy-j avatar Feb 19 '20 09:02 wolfy-j