Luciano Mammino
Luciano Mammino

Reputation: 811

Generate custom proxy methods for doctrine2 entities

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

Answers (1)

Lee Davis
Lee Davis

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:

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/working-with-objects.html#custom-repositories

Upvotes: 1

Related Questions