4

如何使用与另一个选择框相关的选择框的答案?帮助我开始,如果我将第一个选择框的 id 硬编码到第二个选择框中,我的代码就可以工作。但是,如果我尝试使用实际的表单值,我会得到一个

注意:未定义索引:my_group

这是我要基于组选择下拉框显示的下拉框的代码:

  $builder->add('subscriptiontype', 'entity',
        array(
            'label' => 'profile.edit.my_subscription_type', 
            'translation_domain' => 'FOSUserBundle',
            'empty_value' => 'Select subscription type',
            'property' => 'subscription_title',
            'class' => 'SDMarketplaceBundle:SubscriptionType',
            'multiple' => false,
            'attr' => array('onchange' => '',  'class' => ''),
            'query_builder' => function($repository) use ($options){ 
                return $repository
                    ->createQueryBuilder('j')
                        ->where('j.isActive = :active AND j.valid_until > :timenow AND j.group = :group_id')
                        ->setParameter('active', 1)
                        ->setParameter('timenow', new \DateTime('now'))
                        ->setParameter('group_id', $options['my_group'])
                    ->orderBy('j.subscription_title', 'ASC');
                    }
        )
    );

如果我将$options['my_group']更改为作为组 ID 的实际数字,它会正确显示。这是我实际放置 id 代替$options['my_group']时的 html 代码输出:

<select id="fos_user_profile_form_my_group" name="fos_user_profile_form[my_group]" required="required"><option value="">Select group</option><option value="5">administrator</option><option value="2" selected="selected">instructor</option><option value="3">proofreader</option><option value="1">student</option><option value="4">translator</option></select>

对我做错或遗漏的事情有任何帮助吗?

----为我迟到的回复道歉,因为我没有看到你的帖子(但谢谢你。)这是我的 showAction 控制器代码:

public function showAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        $myGroup = $this->getEntityManager()->getRepository('SDMarketplaceBundle:Groups')->findOneBy(array('id' => $user->getMyGroup())); //retrieve the group selected by this user
        $subscriptiontype = $this->getEntityManager()->getRepository('SDMarketplaceBundle:Subscriptiontype')->findOneBy(array('id' => $user->getSubscriptiontype())); //retrieve the subscription type title for this user
        $accounttype = $this->getEntityManager()->getRepository('SDMarketplaceBundle:AccountType')->findOneBy(array('id' => $user->getMyAccountType())); //retrieve the account type name for this user
        return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), 
        array(
            'user' => $user, 
            'myGroup' => $myGroup,
            'subscriptiontype' => $subscriptiontype->getSubscriptionTitle(),
            'accounttype' => $accounttype->getAccountName(),
            'statusCode' => $user->getStatusName($user->getStatusCode())
        ));
    }
4