La Ferme du web - Les forums

Venez baraguiner avec la communauté !

Vous n'êtes pas identifié.

#1 07/05/2014 10:28:52

frond
Membre
Date d'inscription: 07/05/2014
Messages: 4

select widget chainés en symfony2

Salut tout le monde,
Je développe une application symfony2 dans laquelle l'utilisateur est amené à choisir une catégorie de produits dans une select widget, une fois la category sélectionnée, j'aimerai qu'un autre select ( subcategory ) soit automatiquement rempli (les sous-catégorie de la catégorie sélectionnée précédemment).
voici mon code:
foodType

Code:

<?php

namespace Select\BaseBundle\Form;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormInterface;

use Select\BaseBundle\Entity\Food;
use Select\BaseBundle\Entity\Category;
use Select\BaseBundle\Entity\SubCategory;

class FoodType extends AbstractType
{
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
          $builder
            ->add('name')
        ;
                       
        $factory = $builder->getFormFactory();
 
        $refreshSubcategory = function (FormInterface $form, Category $category) use ($factory) {
            $form->add($factory->createNamed('entity', 'subcategory', null, array(
                'class'         => 'Select\BaseBundle\Entity\Subcategory',
                'property'      => 'name',
                'empty_value'   => '-- Select a subcategory --',              
                'query_builder' => function (EntityRepository $repository) use ($category) {
                                       $qb = $repository->createQueryBuilder('subcategory')
                                                        ->innerJoin('subcategory.category', 'category');
 
                                       if($category instanceof Category) {
                                           $qb = $qb->where('subcategory.category = :category')
                                                    ->setParameter('category', $category);
                                       } elseif(is_numeric($category)) {
                                           $qb = $qb->where('category.id = :category_id')
                                                    ->setParameter('category_id', $category);
                                       } else {
                                           $qb = $qb->where('category.id = 1');
                                       }
 
                                       return $qb;
                                   }
                 )));
        };
        
        
        $setCategory = function (FormInterface $form, Category $category) use ($factory)
        {
            $form->add($factory->createNamed('entity', 'category', null, array(
                    'class'         => 'Select\BaseBundle\Entity\Category', 
                    'property'      => 'name', 
                    'property_path' => false,
                    'empty_value'   => '-- Select a category --',
                    'data'          => $category,
                )));
        };
        
  
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($refreshSubcategory) {
            $form = $event->getForm();
            $data = $event->getData();
 
            if($data == null) {
               return; //As of beta2, when a form is created setData(null) is called first
            }
 
            if($data instanceof Category) {
                $category = ($data->getId()) ? $data-> getSubcategory() -> getCategory() : null;
                $refreshSubcategory($form, $category);
                $setCategory($form, $category);
            }
        });
 
        $builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) use ($refreshSubcategory) {
            $form = $event->getForm();
            $data = $event->getData();
 
            if(array_key_exists('category', $data)) {
                $refreshSubcategory($form, $data['category']);
            }
        });  
        
        
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Select\BaseBundle\Entity\Food'
        ));
    }

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

l'entité Category est définie comme suit:

Code:

<?php

namespace Select\BaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;

    /**
     *
     * @ORM\OneToMany(targetEntity="Subcategory", mappedBy="Category")
     */
    private $subcategories;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->subcategories = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add subcategories
     *
     * @param \Select\BaseBundle\Entity\Subcategory $subcategories
     * @return Category
     */
    public function addSubcategory(\Select\BaseBundle\Entity\Subcategory $subcategories)
    {
        $this->subcategories[] = $subcategories;

        return $this;
    }

    /**
     * Remove subcategories
     *
     * @param \Select\BaseBundle\Entity\Subcategory $subcategories
     */
    public function removeSubcategory(\Select\BaseBundle\Entity\Subcategory $subcategories)
    {
        $this->subcategories->removeElement($subcategories);
    }

    /**
     * Get subcategories
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSubcategories()
    {
        return $this->subcategories;
    }

}

l'entité subcategory :

Code:

<?php

namespace Select\BaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Subcategory
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Subcategory
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="subcategories")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;

    /**
     * Inverse Side
     * @ORM\OneToMany(targetEntity="Food", mappedBy="subcategory")
     */
    private $foods;
    
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Subcategory
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->foods = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set category
     *
     * @param \Select\BaseBundle\Entity\Category $category
     * @return Subcategory
     */
    public function setCategory(\Select\BaseBundle\Entity\Category $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \Select\BaseBundle\Entity\Category 
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Add foods
     *
     * @param \Select\BaseBundle\Entity\Food $foods
     * @return Subcategory
     */
    public function addFood(\Select\BaseBundle\Entity\Food $foods)
    {
        $this->foods[] = $foods;

        return $this;
    }

    /**
     * Remove foods
     *
     * @param \Select\BaseBundle\Entity\Food $foods
     */
    public function removeFood(\Select\BaseBundle\Entity\Food $foods)
    {
        $this->foods->removeElement($foods);
    }

    /**
     * Get foods
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getFoods()
    {
        return $this->foods;
    }
}

mon controller:

Code:

<?php

namespace Select\BaseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// Import new namespaces
 
use Select\BaseBundle\Entity\Food;
use Select\BaseBundle\Form\FoodType;

class PageController extends Controller
{
    public function foodAction()
    { 

        $food = new Food();
        $form = $this->createForm(new FoodType($this->getDoctrine()->getManager()), $food);
        
        return $this->render('SelectBaseBundle:Page:food.html.twig', array('form' => $form->createView()));  }

    
    public function getByCategoryIdAction()
    {
        $this->em = $this->get('doctrine')->getManager();
        $this->repository = $this->em->getRepository('SelectBaseBundle:Subcategory');

        $categoryId = $this->get('request')->query->get('data');

        $subcategories = $this->repository->findByCategory($categoryId);

        $html = '';
        foreach($subcategories as $subcategory)
        {
            $html = $html . sprintf("<option value=\"%d\">%s</option>",$subcategory->getId(), $subcategory->getName());
        }

        return new Response($html);
    }

    
}

mon template:

Code:

{# src/Select/BaseBundle/Resources/views/Page/food.html.twig #}
{% extends 'SelectBaseBundle::layout.html.twig' %}

{% block title %}food{% endblock%}

{% block stylesheets %}
    {{ parent() }}

    <link href="{{ asset('bundles/selectbase/css/bootstrap.css') }}" type="text/css" rel="stylesheet" />
    <style type="text/css">
        .row-delete {
            display: inline-block;
            width: 10px;
        }
    </style>
{% endblock %}

{% block body %}

    <header>
        <h1>Food page for database</h1>

    </header>
    
    {{ form(form) }}

{% endblock %}

{% block javascripts %}
        {{ parent() }}
        <script type="text/javascript" src="{{ asset('bundles/selectbase/js/jquery-1.6.2.js')}}"></script>
        <script>
            $('#category').change( function() {
            $.ajax({
                type: "POST",
                data: "data=" + $(this).val(),
                url:"{{ path('SelectBaseBundle_subcategoryByCategory') }}",
                success: function(msg){
                    if (msg != ''){
                       $('#subcategory').html(msg).show();
                       }
                       else
                       {
                           $('#subcategory').html('<em>No item result</em>');
                       }
                   }
               });
          });

        </script>

{% endblock %}

Hors ligne

 

07/05/2014 10:28:52

Botte De Foin Publicitaire


#2 26/05/2014 15:36:10

frond
Membre
Date d'inscription: 07/05/2014
Messages: 4

Re: select widget chainés en symfony2

le tuto du lien suivant m'a aidé à résoudre mon problème qui était dù à une condition if jamais vérifiée.

lien: http://showmethecode.es/php/symfony/sym … e-eventos/

Hors ligne

 

#3 14/07/2015 05:46:41

gassibe
Membre
Date d'inscription: 13/07/2015
Messages: 4

Re: select widget chainés en symfony2

OK, je vais le voir. Merci pour le lien. etui iphone 6s housse iphone 6s

Dernière modification par gassibe (20/07/2015 09:20:28)

Hors ligne

 

Pied de page des forums

Powered by FluxBB
© Copyright 2008-2009 - LaFermeduWeb.net