Reputation: 811
I have the following scenario:
Users may follow or be followed by other users.
Users are related by a ManyToMany relationship as the following code shows
/**
@var ArrayCollection $follows
@ORM\ManyToMany(targetEntity="User", inversedBy="followers")
@ORM\JoinTable(name="user_follows",
joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns={@ORM\JoinColumn(name="followed_user_id", referencedColumnName="id")}
)
*/
protected $follows;
/**
@var ArrayCollection $followers
@ORM\ManyToMany(targetEntity="User", mappedBy="follows")
*/
protected $followers;
I'd like to have a collection field named followersIDs
(not persisted to the database) that will be populated on demand (when calling $user->getFollowersIDs()
) with only the ids (not the whole entities) of the users who follow the current user.
The point is that a user can be followed by a huge amount of other users and i think it's pointless to load all the users entities when i just need their ids for some quick checks. I wonder if it's possible to create a custom proxy method to define this custom querying logic or if some other solution exists.
Thanks in advice.
Upvotes: 0
Views: 953
Reputation: 4756
Don't touch the Doctrine proxy classes, they're generated and used internally. Instead looks at writing some custom DQL for the data you require and putting it into a repository class.
A good example for setting up a repository class is here:
Upvotes: 1