Pages

Affichage des articles dont le libellé est Symfony. Afficher tous les articles
Affichage des articles dont le libellé est Symfony. Afficher tous les articles

Symfony 2/doctrine2 formulaire de recherche par choix multiple sujet

samedi 1 février 2014




Bonjour à tous,

J'ai pas trouvé ma solution même sur le forum donc je post ce sujet :

j'ai une recherche de base pour mon formulaire dont j'ai ajouté des checkbox multiples.

Lorsque je sélectionne un checkbox, cela fonctionne.Par contre lorsque je sélectionne deux il me met cette erreur... et comme je débute ...

Code:


[2/2]DBALException: An exception occurred while executing 'SELECT j0_.id AS id0, j0_.intitule AS intitule1, j0_.categorie AS categorie2, j0_.type AS type3, j0_.adresse AS adresse4, j0_.remuneration AS remuneration5, j0_.date AS date6, j0_.description AS description7 FROM job j0_ WHERE j0_.type = ?, ?' with params ["bricolage", "informatique"]:
 
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'informatique'' at line 1


Je sais que c'est ma requete mais avec createQueryBuilder() je ne sais comment faire ... pourtant j'ai regardé les exemples et j'ai rien compris ... :-)

Dans mon repository :

Code:


public function findListArticles(){
    $qb = $this->_em->createQueryBuilder();
    return $qb->select('monjob')->from('LcMonjobBundle:Job', 'monjob');
}
 
public function findAnnonceByParametres($data)
    {
        $query = $this->createQueryBuilder('monjob');
 
        $query->where('monjob.type = :type')
     
            ->setParameters(array(
                'type' => $data['type'],));
 
        return $query->getQuery()->getResult();
    }
}


Mon formulaire :

Code:


public function buildForm(FormBuilderInterface $builder, array $options)
    {
 
$builder
            ->add('intitule', 'text', array('label'=>'Mon intitule'))
            ->add('categorie')
            ->add('type', 'choice', array('expanded' => true, 'multiple' => true, 'choices' => array('bricolage' => 'bricolage', 'informatique' => 'informatique')))
            ->add('date')
            ->add('adresse')
            ->add('remuneration')
            ->add('description')
        ;
    }
   
    /**
    * @param OptionsResolverInterface $resolver
    */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Lc\MonjobBundle\Entity\job'
        ));
    }
 
    /**
    * @return string
    */
    public function getName()
    {
        return 'lc_monjobbundle_job';
       
    }
}


Éventuellement ma fonction de mon controller:

Code:


public function jobSearchAction(){
//$recupJob=$em->getRepository('LcMonjobBundle:Job')->findAll();
//return $this->render('LcMonjobBundle:Default:voir.html.twig',array('recupJob'=>$recupJob,));
 
$form = $this->createForm(new jobType());
    $request = $this->getRequest();
    if($request->getMethod() == 'POST')
        {
            $form->bind($request);
 
            //On vérifie que les valeurs entrées sont correctes
            if($form->isValid())
            {
                $em = $this->getDoctrine()->getManager();
 
                //On récupère les données entrées dans le formulaire par l'utilisateur
                $data = $this->getRequest()->request->get('lc_monjobbundle_job');
 
                //On va récupérer la méthode dans le repository afin de trouver toutes les annonces filtrées par les paramètres du formulaire
                $recupJob = $em->getRepository('LcMonjobBundle:Job')->findAnnonceByParametres($data);
                return $this->render('LcMonjobBundle:Default:listjobfind.html.twig', array('recupJob' => $recupJob,));
  }
        }
 
    return $this->render('LcMonjobBundle:Default:listjob.html.twig', array('form' => $form->createView()));
}


j'ai fait au plus simple, avec le fichier du repository au niveau de createQueryBuilder , et le formulaire ( ligne de choice ).

merci d'avance

lo




Symfony et formulaire sujet

mercredi 29 janvier 2014




Bonjour,
je suis actuellement sur un projet avec symfony, je dois créer un bundle qui permet d'enregistrer un produit d'un concurrent pour comparer son prix,je bloque sur le formulaire, mon formulaire est présenté de cette manière :

Nom : (nom du produit sur ma boutique)
Ref : (ref du produit de ma boutique)
Url du produit : (Url du produit concurrent)
Type: (categorie du produit)
Script : (choix du script a executer pour ce produit celon le concurrent)

le soucis est que si j'ai les deux meme produit chez deux concurrents ou plus je peux mettre qu'une url est il possible d'avoir un ajout automatique d'une colonne dans la base de donnée ainsi qu'un input url_nom du concurrent sans devoir à chaque fois retoucher le code mais entitées sont faite comme ceci :


Code:


<?php
namespace noza\concurrentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
 class Produit
 {
        /**
        * @ORM\GeneratedValue
    * @ORM\Id
    * @ORM\Column(type="integer")
    */
        private $id;
       
        /**   
        * @ORM\Column(type="string",length=255)
        * @Assert\NotBlank()
        */
        private $nom;
       
        /**
        *@ORM\JoinTable(name="produit_type")
        * @ORM\ManyToOne(targetEntity="Type")
    */   
        private $type;
       
        /**
    * @ORM\Column(type="string",length=14)
    */
        private $refdivalto;
       
        /**
        *@ORM\JoinTable(name="produit_script")
    * @ORM\ManyToOne(targetEntity="Script")
    */
        private $script;
       
          /**
    * @ORM\Column(type="string",length=255)
    */
        private $url_produit;

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

    /**
    * Set nom
    *
    * @param string $nom
    * @return Produit
    */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
    * Get nom
    *
    * @return string
    */
    public function getNom()
    {
        return $this->nom;
    }

    /**
    * Set refdivalto
    *
    * @param string $refdivalto
    * @return Produit
    */
    public function setRefdivalto($refdivalto)
    {
        $this->refdivalto = $refdivalto;

        return $this;
    }

    /**
    * Get refdivalto
    *
    * @return string
    */
    public function getRefdivalto()
    {
        return $this->refdivalto;
    }

    /**
    * Set url_produit
    *
    * @param string $urlProduit
    * @return Produit
    */
    public function setUrlProduit($urlProduit)
    {
        $this->url_produit = $urlProduit;

        return $this;
    }

    /**
    * Get url_produit
    *
    * @return string
    */
    public function getUrlProduit()
    {
        return $this->url_produit;
    }

    /**
    * Set type
    *
    * @param \noza\concurrentBundle\Entity\Type $type
    * @return Produit
    */
    public function setType(\noza\concurrentBundle\Entity\Type $type = null)
    {
        $this->type = $type;

        return $this;
    }

    /**
    * Get type
    *
    * @return \noza\concurrentBundle\Entity\Type
    */
    public function getType()
    {
        return $this->type;
    }

    /**
    * Set script
    *
    * @param \noza\concurrentBundle\Entity\Script $script
    * @return Produit
    */
    public function setScript(\noza\concurrentBundle\Entity\Script $script = null)
    {
        $this->script = $script;

        return $this;
    }

    /**
    * Get script
    *
    * @return \noza\concurrentBundle\Entity\Script
    */
    public function getScript()
    {
        return $this->script;
    }
}




Code:


<?php
namespace noza\concurrentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Script
{
        /**
        * @ORM\GeneratedValue
        * @ORM\Id
        * @ORM\Column(type="integer")
        */       
        private $id;
       
        /**   
        * @ORM\Column(type="string",length=255)
        * @Assert\NotBlank()
        */
        private $nom;

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

    /**
    * Set nom
    *
    * @param string $nom
    * @return Script
    */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
    * Get nom
    *
    * @return string
    */
    public function getNom()
    {
        return $this->nom;
    }
}




Code:


<?php
namespace noza\concurrentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
 class Type
 {
        /**
        * @ORM\GeneratedValue
    * @ORM\Id
    * @ORM\Column(type="integer")
    */
        private $id;
       
        /**
    * @ORM\Column(type="string",length=255)
    * @Assert\NotBlank()
    */   
        private $nom;

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

    /**
    * Set nom
    *
    * @param string $nom
    * @return Type
    */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
    * Get nom
    *
    * @return string
    */
    public function getNom()
    {
        return $this->nom;
    }
}


il faut imaginé que script n'existe plus et que dans mon formulaire il y ai url_nomconcurent1 et par magie si il y a une nouveau concurrent dans la base de donnée concurrent un input url_concurrent2 apparraisse