0

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.

4

1 回答 1

1

不要碰 Doctrine 代理类,它们是在内部生成和使用的。相反,着眼于为您需要的数据编写一些自定义 DQL 并将其放入存储库类中。

设置存储库类的一个很好的例子是:

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

于 2012-05-18T08:00:18.080 回答