如何在Symfony 2

时间:2015-06-28 02:22:44

标签: forms symfony doctrine

我在Symfony 2项目中正在处理的表单遇到问题。我正在尝试创建一个选择,它将在我的数据库中显示我的所有项目。

现在,我的选择只显示我的项目的ID,但我想要的是显示我的项目的名称,但id具有该选项的值。我创建了一个实体类型的字段,但我需要做一个query_builder因为我有一个实体项目,但我的项目名称在另一个实体中,因为我的项目名称可以是英文或法文。 / p>

我尝试了几件事,但我无法得到怀疑的查询!

我需要获取我的项目ID和法语名称

我的项目实体:

<?php

namespace PublicBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Projet
 *
 * @ORM\Table(name="pt_projet");
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot")
 */
class Projet
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //ID du projet
    protected $id;

     /**
     * @ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projet", orphanRemoval=true)
     */
    protected $descriptions;

    /**
     * @ORM\Column(name="pro_img", type="string", length=64, unique=true)
     */
    //Nom du fichier de l'image du projet
    protected $image;

    /**
     * @ORM\Column(name="pro_technologie_utilisee", type="text", length=200)
     */
    //Text qui liste tout les technologies utilisées pour le projet
    protected $technologie;

    /**
     * @ORM\Column(name="pro_annee", type="integer", length=4)
     */
    //Année de réalisation du projet
    protected $annee;

    /**
     * @ORM\ManyToOne(targetEntity="Type", inversedBy="projets")
     * @ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false)
     */
    //Clef étrangère du type de projet
    //Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio.
    protected $type;

    /**
     * @ORM\Column(name="pro_fichier", type="string", length=64, unique=true)
     */
    //Nom du fichier du projet
    private $fichier;

    /**
     * @ORM\Column(name="pro_largeur", type="integer")
     */
    //Largeur du projet
    protected $largeur;

    /**
     * @ORM\Column(name="pro_hauteur", type="integer")
     */
    //Hauteur du projet
    protected $hauteur;

    /**
    * @ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"})
    */
    //La ou les catégories du projet
    private $categories;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->descriptions=new ArrayCollection();
        $this->categories=new ArrayCollection();
    }


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

    /**
     * Set image
     *
     * @param string $image
     * @return Projet
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

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

    /**
     * Set technologie
     *
     * @param string $technologie
     * @return Projet
     */
    public function setTechnologie($technologie)
    {
        $this->technologie = $technologie;

        return $this;
    }

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

    /**
     * Set annee
     *
     * @param integer $annee
     * @return Projet
     */
    public function setAnnee($annee)
    {
        $this->annee = $annee;

        return $this;
    }

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

    /**
     * Set fichier
     *
     * @param string $fichier
     * @return Projet
     */
    public function setFichier($fichier)
    {
        $this->fichier = $fichier;

        return $this;
    }

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

    /**
     * Set largeur
     *
     * @param integer $largeur
     * @return Projet
     */
    public function setLargeur($largeur)
    {
        $this->largeur = $largeur;

        return $this;
    }

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

    /**
     * Set hauteur
     *
     * @param integer $hauteur
     * @return Projet
     */
    public function setHauteur($hauteur)
    {
        $this->hauteur = $hauteur;

        return $this;
    }

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

    /**
     * Add descriptions
     *
     * @param \PublicBundle\Entity\ProjetInt $descriptions
     * @return Projet
     */
    public function addDescription(\PublicBundle\Entity\ProjetInt $descriptions)
    {
        $this->descriptions[] = $descriptions;

        return $this;
    }

    /**
     * Remove descriptions
     *
     * @param \PublicBundle\Entity\ProjetInt $descriptions
     */
    public function removeDescription(\PublicBundle\Entity\ProjetInt $descriptions)
    {
        $this->descriptions->removeElement($descriptions);
    }

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

    /**
     * Set type
     *
     * @param \PublicBundle\Entity\Type $type
     * @return Projet
     */
    public function setType(\PublicBundle\Entity\Type $type)
    {
        $this->type = $type;

        return $this;
    }

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

    /**
     * Add categories
     *
     * @param \PublicBundle\Entity\Categorie $categories
     * @return Projet
     */
    public function addCategory(\PublicBundle\Entity\Categorie $categories)
    {
        $this->categories[] = $categories;

        return $this;
    }

    /**
     * Remove categories
     *
     * @param \PublicBundle\Entity\Categorie $categories
     */
    public function removeCategory(\PublicBundle\Entity\Categorie $categories)
    {
        $this->categories->removeElement($categories);
    }

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

我项目所有文本的实体。 'langue'字段必须是'fr'(langue ='fr'),名称是'nom'字段

<?php

namespace PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Projet Inter
 *
 * @ORM\Table(name="pt_projet_int");
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetIntDepot")
 */
class ProjetInt
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //ID du projet
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Projet", inversedBy="descriptions")
     * @ORM\JoinColumn(name="pro_id_CE", referencedColumnName="id", nullable=false)
     */
    //Clef étrangère du projet associé
    protected $projet;

    /**
     * @ORM\Column(name="pri_lang", type="string", length=2)
    */
    //Langue de la carégorie
    protected $langue;

    /**
     * @ORM\Column(name="pri_nom", type="string", length=30)
     */
    //Nom du produit
    protected $nom;

    /**
     * @ORM\Column(name="pri_description_cours", type="text",length=100)
     */
    //Description cours du projet
    protected $descriptionCours;

    /**
     * @ORM\Column(name="pri_description_complete", type="text",length=250)
     */
    //Description complete du projet
    protected $descriptionComplete;

    /**
     * @ORM\Column(name="pri_roles", type="string",length=60)
     */
    //Roles joués dans la création du projet
    protected $roles;

    /**
     * @ORM\Column(name="pri_aptitudes_developpees", type="string",length=200)
     */
    //Aptitudes développées lors du projet
    protected $aptitudesDeveloppees;


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

    /**
     * Set langue
     *
     * @param string $langue
     * @return ProjetInt
     */
    public function setLangue($langue)
    {
        $this->langue = $langue;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set descriptionCours
     *
     * @param string $descriptionCours
     * @return ProjetInt
     */
    public function setDescriptionCours($descriptionCours)
    {
        $this->descriptionCours = $descriptionCours;

        return $this;
    }

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

    /**
     * Set descriptionComplete
     *
     * @param string $descriptionComplete
     * @return ProjetInt
     */
    public function setDescriptionComplete($descriptionComplete)
    {
        $this->descriptionComplete = $descriptionComplete;

        return $this;
    }

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

    /**
     * Set roles
     *
     * @param string $roles
     * @return ProjetInt
     */
    public function setRoles($roles)
    {
        $this->roles = $roles;

        return $this;
    }

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

    /**
     * Set aptitudesDeveloppees
     *
     * @param string $aptitudesDeveloppees
     * @return ProjetInt
     */
    public function setAptitudesDeveloppees($aptitudesDeveloppees)
    {
        $this->aptitudesDeveloppees = $aptitudesDeveloppees;

        return $this;
    }

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

    /**
     * Set projetId
     *
     * @param \PublicBundle\Entity\Projet $projetId
     * @return ProjetInt
     */
    public function setProjetId(\PublicBundle\Entity\Projet $projetId)
    {
        $this->projetId = $projetId;

        return $this;
    }

    /**
     * Get projetId
     *
     * @return \PublicBundle\Entity\Projet 
     */
    public function getProjetId()
    {
        return $this->projetId;
    }

    /**
     * Set projet
     *
     * @param \PublicBundle\Entity\Projet $projet
     * @return ProjetInt
     */
    public function setProjet(\PublicBundle\Entity\Projet $projet)
    {
        $this->projet = $projet;

        return $this;
    }

    /**
     * Get projet
     *
     * @return \PublicBundle\Entity\Projet 
     */
    public function getProjet()
    {
        return $this->projet;
    }
}

我的表格:

<?php

namespace AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

Class ProjetsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $constructeur, array $options)
    {
        $constructeur
        ->add('Ajouter', 'submit', array('label'=>'Ajouter un projet'))
        ->add('Projet', 'entity', [
            'label'=>false,
            'class'=>'PublicBundle\Entity\Projet',
            'property'=>'id',
            'required'=>true,
            'query_builder'=>function(EntityRepository $repository){
                return $repository->createQueryBuilder('p')
                                    ->innerJoin('p.descriptions', 'd')
                                    ->where("d.langue='fr'")
                                    ->groupBy('p.id');
            },
        ])
        ->add('Modifier', 'submit')
        ->add('Supprimer', 'submit');
    }

    public function getName()
    {

        return 'projets_type';

    }
}

2 个答案:

答案 0 :(得分:1)

我刚刚找到解决方案,就像ihsan所说,但我需要更改查询:

->add('Projet', 'entity', [
            'label'=>false,
            'class'=>'PublicBundle\Entity\Projet',
            'property'=>'descriptions[0].nom',
            'required'=>true,
            'query_builder'=>function(EntityRepository $repository){
                return $repository->createQueryBuilder('p')
                                    ->innerJoin('p.descriptions', 'd')
                                    ->where("d.langue='fr'")
                                    ->groupBy('p.id');
            },
        ])

此外,我的选项的值是项目的ID,正如我想要的那样。

答案 1 :(得分:0)

property选项是用于显示选项的属性路径。因此,您可以使用PropertyAccessor组件

支持的任何内容

因此,使用它来访问您的项目名称。

->add('Projet', 'entity', [ 
    ...
    'property'=>'descriptions.nom',
    ...
])