I have 5 Entities:
My goal is to display a list of choice fields where I can choose all the UserAffiliations which are not in PersonAffiliations.
My idea is to create a public function in the UserAffiliationRepository which will return only those affiliations for a specific user which are not preset for a specific person.
For that, I am using:
class UserAffiliationRepository extends EntityRepository
{
public function getUnselectedAffiliations( $user_id = null, $person_id = null )
{
$commQB = $this->createQueryBuilder( 'ua' )
->select('ua');
$commQB->where("ua.user_id = {$user_id}");
$commQB->andWhere( "ua.affiliation_id not in ( select pa.affiliation_id FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = 3077 )" );
return $commQB->getQuery()->getResult();
}
}
And this works fine.
Now, I would like to use this result in a FormBuilder. For that, In my controller, I am using:
$affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
$enquiry = new PersonAffiliation();
$formType = new SubmissionAffiliationAddFormType($user, $affiliations);
$form = $this->createForm($formType, $enquiry);
And then in the Form class, I am using:
$builder->add('affiliation', 'entity', array(
'class' => 'SciForumVersion2Bundle:UserAffiliation',
'multiple' => true));
But here, I am getting all the affiliations for the specific user, not only thos ones which are not allready in the PersonAffiliations entity.
Any help? Thank you.
You have to migrate your getUnselectedAffiliations
function directly into entity_type in the following way
$builder->add('affiliation', 'entity', array(
'class' => 'SciForumVersion2Bundle:UserAffiliation',
'multiple' => true,
'query_builder' = function(EntityRepository $repo) use ($yourParameters){
return $repo->createQueryBuilder(....);}));
if you want to pass $yourParameters
, you have to do that into __construct
function (implement it, if you don't have one) and when you create your form, you can pass them along the calls