I have a Company class that references users:
/**
* @MongoDB\Document()
*/
class Company {
/* ... */
/**
* @MongoDB\ReferenceMany(targetDocument="Topboard\UserBundle\Document\User", inversedBy="companies")
*/
protected $users;
}
In my controller I need to check if the reference to the user exists in the company and keep only the reference to that user, not other references. I also want to avoid multiple DB requests for users. I just want to check if the id of the references matches $currentUserId
.
public function getCompanyAction($companyId, $currentUserId) {
$dm = $this->get('doctrine_mongodb')->getManager();
$company = $dm->getRepository( 'TopboardAppBundle:Company' )->findOneById( $companyId );
foreach ($company->getUsers() as $user) {
// Foreach will query each user separetly. This is not what I need.
// I need somehow access array of references' ids
// and compare them with the $currentUserId
if($user->getId() !== $currentUserId){
// Remove reference
}
}
return $company;
}