getId() lazy load entity if getId() is in trait #5263

Closed
opened 2026-01-22 15:02:53 +01:00 by admin · 7 comments
Owner

Originally created by @steevanb on GitHub (Sep 19, 2016).

Originally assigned to: @Ocramius on GitHub.

Hi !

If i use a trait to define getId(), instead of directly define it in class, Doctrine will trigger an useless lazy load.

Here is a test case with 3 simples entities :

class User
{
    /** @OneToMany(targetEntity="Comment", mappedBy="user") */
    protected $comments;

    /** @OneToMany(targetEntity="CommentWithTrait", mappedBy="user") */
    protected $commentsWithTrait;

    public function getComments()
    {
        return $this->comments;
    }

    public function getCommentsWithTrait()
    {
        return $this->commentsWithTrait;
    }
}
class Comment
{
    protected $id;

    public function getId()
    {
        return $this->id;
    }

    /** @ManyToOne(targetEntity="User") */
    protected $user;
}
trait IdTrait
{
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

class CommentWithTrait
{
    use IdTrait;

    /** @ManyToOne(targetEntity="User") */
    protected $user;
}

Now, try to call Comment::getId() and CommentWithTrait::getId() :

$user = $repository->find(1);

// this will not trigger lazy loading of Comment, cause id is already defined in Proxy
$user->getComments()[0]->getId();

// this will trigger lazy loading of CommentWithTrait,
// cause getId() method is not directly defined in CommentWithTrait, but in IdTrait
$user->getCommentsWithTrait()[0]->getId();

Thanks !

Originally created by @steevanb on GitHub (Sep 19, 2016). Originally assigned to: @Ocramius on GitHub. Hi ! If i use a trait to define getId(), instead of directly define it in class, Doctrine will trigger an useless lazy load. Here is a test case with 3 simples entities : ``` php class User { /** @OneToMany(targetEntity="Comment", mappedBy="user") */ protected $comments; /** @OneToMany(targetEntity="CommentWithTrait", mappedBy="user") */ protected $commentsWithTrait; public function getComments() { return $this->comments; } public function getCommentsWithTrait() { return $this->commentsWithTrait; } } ``` ``` php class Comment { protected $id; public function getId() { return $this->id; } /** @ManyToOne(targetEntity="User") */ protected $user; } ``` ``` php trait IdTrait { protected $id; public function getId() { return $this->id; } } class CommentWithTrait { use IdTrait; /** @ManyToOne(targetEntity="User") */ protected $user; } ``` Now, try to call Comment::getId() and CommentWithTrait::getId() : ``` php $user = $repository->find(1); // this will not trigger lazy loading of Comment, cause id is already defined in Proxy $user->getComments()[0]->getId(); // this will trigger lazy loading of CommentWithTrait, // cause getId() method is not directly defined in CommentWithTrait, but in IdTrait $user->getCommentsWithTrait()[0]->getId(); ``` Thanks !
admin added the BugCan't Fix labels 2026-01-22 15:02:53 +01:00
admin closed this issue 2026-01-22 15:02:53 +01:00
Author
Owner

@steevanb commented on GitHub (Oct 31, 2016):

Nobody's here ?

@steevanb commented on GitHub (Oct 31, 2016): Nobody's here ?
Author
Owner

@Ocramius commented on GitHub (Oct 31, 2016):

This is a known bug - https://github.com/doctrine/common/pull/337

See https://github.com/doctrine/doctrine2/pull/1241

@Ocramius commented on GitHub (Oct 31, 2016): This is a known bug - https://github.com/doctrine/common/pull/337 See https://github.com/doctrine/doctrine2/pull/1241
Author
Owner

@steevanb commented on GitHub (Oct 31, 2016):

Why can't you fix it ?

@steevanb commented on GitHub (Oct 31, 2016): Why can't you fix it ?
Author
Owner

@Ocramius commented on GitHub (Oct 31, 2016):

Mostly because reflection+traits is a mess. It might be fixed by replacing
proxying library support with ProxyManager though

On 31 Oct 2016 12:40, "Steevan BARBOYON" notifications@github.com wrote:

Why can't you fix it ?


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6042#issuecomment-257272710,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakJU45eVbGTdtBrHjwS06nwnXAUIgks5q5dOXgaJpZM4KAdzK
.

@Ocramius commented on GitHub (Oct 31, 2016): Mostly because reflection+traits is a mess. It might be fixed by replacing proxying library support with ProxyManager though On 31 Oct 2016 12:40, "Steevan BARBOYON" notifications@github.com wrote: > Why can't you fix it ? > > — > You are receiving this because you modified the open/close state. > Reply to this email directly, view it on GitHub > https://github.com/doctrine/doctrine2/issues/6042#issuecomment-257272710, > or mute the thread > https://github.com/notifications/unsubscribe-auth/AAJakJU45eVbGTdtBrHjwS06nwnXAUIgks5q5dOXgaJpZM4KAdzK > .
Author
Owner

@Metabor commented on GitHub (Jul 10, 2019):

For me the easiest workaround was to make getId() method in trait final, so that the proxy class will not overwrite the original code.

@Metabor commented on GitHub (Jul 10, 2019): For me the easiest workaround was to make getId() method in trait final, so that the proxy class will not overwrite the original code.
Author
Owner

@Ocramius commented on GitHub (Jul 10, 2019):

This is already fixed in 3.x-dev

@Ocramius commented on GitHub (Jul 10, 2019): This is already fixed in `3.x-dev`
Author
Owner

@Metabor commented on GitHub (Jul 10, 2019):

Good to hear this. So I am happy that I can remove my workaround after this fix is in a release version.

@Metabor commented on GitHub (Jul 10, 2019): Good to hear this. So I am happy that I can remove my workaround after this fix is in a release version.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5263