0

I have list developer, not all developer, developer who have some parameters by filter and I need create check box for this some developer and post developer in another action, in this action identification developer who have true checkbox, I try this but have null check develoepr

class SelectByIdentityType extends AbstractType
{
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('id', 'entity', array(
                'required' => false,
                'class' => 'ArtelProfileBundle:Developer',
                'property' => 'id',
                'property_path' => '[id]', # in square brackets!
                'multiple' => true,
                'expanded' => true
            ));
        }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => null,
                'csrf_protection' => false
            ));
        }

        /**
         * @return string
         */
        public function getName()
        {
            return 'select';
        }
}

and action where I visible my developers

   /**
 * Finds and displays a Project entity.
 *
 * @Route("/{id}/show", name="admin_project_show")
 * @Method({"GET", "POST"})
 * @Template()
 * @ParamConverter("entity", class="ArtelProfileBundle:Project")
 */
public function showAction($entity, Request $request)
{
   //some logic
            $developers = $profileRepository->findBySkillsAndTag($developer, $skill_form);
        $form = $this
            ->createForm(
                new SelectByIdentityType(),
                $developers
            )
            ->createView()

    return array(
        'entity'      => $entity,
        'form' => $form,
        'developers'  => $developers,
    );
}

and if I check developer click SEND and then in the controller action which receives the POSTed data:

  /**
 * Send email for Developers.
 *
 * @Route("/{id}/send", name="admin_project_send_email")
 * @Method({"GET", "POST"})
 * @Template()
 * @ParamConverter("entity", class="ArtelProfileBundle:Project")
 */
public function sendAction($entity, Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $developer = $request->getSession()->get('developer');
    $form = $this
        ->createForm(new SelectByIdentityType())
    ;
    $form->bind($request);
    if ($form->isValid()) {
        $data = $form->getData();//$data['id'] I have 0
        $ids  = array();
        foreach ($data['id'] as $developer) {
            $ids[] = $entity->getId();
        }
        $request->getSession()->set('admin/foo_list/batch', $ids);
    }

Why in $data['id'] I have 0 ?? and my template

{% for entity in developers %}
            <tr>
                <td>{{ form_widget(form.id[entity.id]) }}</td>

Any ideas ??

1 Answer 1

1

You can filter the entities displayed in an entity field by using the query_builder option. That way you can just normally render the whole form and you get a checkbox list of only the entities that your query returns. If you need to display it in a special way, you can override the form templates.

Unfortunately, as far as I know you can't use DQL or repository methods in the query_builder option. You have to write the query-builder code directly into the form definition.

An example:

$builder->add('id', 'entity', array(
            'required' => false,
            'class' => 'ArtelProfileBundle:Developer',
            'multiple' => true,
            'expanded' => true
            'query_builder' => function($repo)  
                {
                    return $repo->createQueryBuilder('d')
                           ->where('x = y');
                }
));
Sign up to request clarification or add additional context in comments.

1 Comment

A note: $repo is an instance of the repository mapped to 'ArtelProfileBundle:Developer'. You are free to use pre-defined repository method as well, as long as it does not return a direct result, but a query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.