Filter listing in Symfony2?

Hello, I tried Symfony and I am very new. I am looking for an elegant way to filter lists.

Let me explain:

I have two objects: Link and Tag. They are in several respects.

In my index view, I created this form. I did findAll () to get all my tags for selection:

<form method="GET" action="">
    <input class="btn btn-default" type="submit"/>
    <select name="tags[]" class="selectpicker" multiple="yes">
        {% for tag in tags %}
            <option value="{{ tag.id }}"> {{ tag.title }}</option>
        {% endfor %}
    </select>
</form>

This is how I can capture the whole DESC link order:

$links = $em->getRepository('TestDefaultBundle:Link')->findBy(
    array(),
    array('id' => 'desc')
);

How can I collect the selected tags (in the controller) and capture all link filters with these selected tags.

Another question that I know, we can create a form for an object, but what about this form?

EDIT

This is my indexAction:

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $tags = $em->getRepository('LanCrmBundle:LinkTag')->findAll();

    // Create the filter form.
    $form = $this->createFormBuilder()
        ->add('tags', 'entity', array(
            'class' => 'LanCrmBundle:LinkTag',
            'multiple' => true,
            'expanded' => false,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.title', 'ASC');
            }
        ))
        ->add('OK', 'submit')
        ->getForm()
    ;

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

        // Get all links filtered by tags.
        // How to use the $data to filter my links?
        $links = $em->getRepository('LanCrmBundle:Link')->findBy(
            array(),
            array('id' => 'desc')
        );
    } else {
        // Get all links.
        $links = $em->getRepository('LanCrmBundle:Link')->findBy(
            array(),
            array('id' => 'desc')
        );
    }

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $links,
        $this->get('request')->query->get('page', 1),
        4
    );

    return $this->render('LanCrmBundle:Link:index.html.twig', array(
        'pagination' => $pagination,
        'tags' => $tags,
        'form' => $form->createView()
    ));
}

I have this error:

"__toString()" "Lan\CrmBundle\Entity\LinkTag", . , "" .

StringCastException: "__toString()" "Lan\CrmBundle\Entity\LinkTag", . , "" .

+4
2

. https://github.com/lexik/LexikFormFilterBundle

Guesser Bundle, lexik filterForm EntityFormType . https://github.com/juanmf/FilterTypeGuesserBundle

( README.md) .

    private function createFilterForm($docType)
    {
        $adapter = $this->get('dd_form.form_adapter');
        $type = $this->getFormForDocument($this->getClassFromDocType($docType));
        return $adapter->adaptForm(
            $type,
            $this->generateUrl('document_search', array('docType' => $docType)),
            array('pdfPath', 'pdfPages', 'batchStatus', 'createdInBatch', 'documentType')
            );
    }

: Sf >= 2.8 formTypes FQCN, typeGuesser . .

+3

- Symfony forms.

:

$form = $this->createFormBuilder()
        ->add('tag', 'entity', array(
            'class' => 'TestDefaultBundle:YoutTagEntity',
            'multiple' => true,
            'expanded' => false,
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('u')
                    ->orderBy('u.title', 'ASC')
            },))
        ->add('OK', 'submit')
        ->getForm();

    $form->handleRequest($request);
    if ($form->isValid()) {
            // data is an array of values from form, for example: $data['tag']
            $data = $form->getData();

        //here you can now pass variables to another query
        $links = $em->getRepository('TestDefaultBundle:Link')->findBy(
            array(),
            array('id' => 'desc')
        );
    }

    // ... render the form

:

{{ form(form) }}
+1

All Articles