恢复值选择imput相关实体doctrine symfony 2

时间:2016-05-12 11:09:07

标签: symfony doctrine-orm query-builder entities formbuilder

我正在尝试根据Symfony 2的学说中的相关实体创建一个表单。我有不同的实体:Tematica,Personal,Hilo,Consultante,Consulta,Consulta_Asignatura和Asignatura。我仍然坚持我的所有数据,但与实体Hilo Personal和Tematica有一些问题。 在我的表单中需要显示select imput,它应该包含所有“Enunciado”行,然后在此选择输入中恢复所选选项的电子邮件。 我会分享我的代码,希望有人可以帮助我。

class HiloType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('consultanteemail', new ConsultanteType(), array ('label' => false))
            ->add('personalemail', 'entity', array(
                'label' => 'Personal de Soporte',
                'class' => 'GuiasDocentes\AppBundle\Entity\Personal',
                'property' => 'Email',
                'by_reference' => 'false',
                'query_builder' => function(PersonalRepository $pr) {
                    $query= $pr->createQueryBuilder('u')
                    ;
                    return $query;
                },
                'empty_value' => 'Elige un perfil de consulta:',
                ))
            // ->add('personalemail', new PersonalType(), array ('label' => false))
        ;
    }
 * Personal
 *
 * @ORM\Table(name="personal")
 * @ORM\Entity(repositoryClass="GuiasDocentes\AppBundle\Entity\PersonalRepository")

class Personal
{
    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=50, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="nombre", type="string", length=30, nullable=true)
     */
    private $nombre;

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

    /**
     * @var string
     *
     * @ORM\Column(name="departamento", type="string", length=20, nullable=true)
     */
    private $departamento;

    /* Customized code */
    /**
     * @ORM\OneToMany(targetEntity="GuiasDocentes\AppBundle\Entity\TematicaSoporte", mappedBy="personalEmail")
     * @Assert\Valid()
     */    
    private $tematicasSoporte;

    public function __constructor(){
        $this->tematicasSoporte = new ArrayCollection();
        $this->hilos = new ArrayCollection();
    }

    public function addTematicasSoporte(\GuiasDocentes\AppBundle\Entity\TematicaSoporte $tematicaSoporte){
        $this->tematicasSoporte[] = $tematicaSoporte;
        return $this;
    }    

    public function setTematicasSoporte(\GuiasDocentes\AppBundle\Entity\TematicaSoporte $tematicaSoporte){
        $this->tematicasSoporte[] = $tematicaSoporte;
        return $this;
    }

    public function getTematicasSoporte(){
        return $this->tematicasSoporte;
    }

    /**
     * @ORM\OneToMany(targetEntity="GuiasDocentes\AppBundle\Entity\Hilo", mappedBy="personalemail")
     * @Assert\Valid()
     */    
    private $hilos;

    public function setHilos(Hilo $hilo){
        $this->hilos[] = $hilo;
        return $this;
    }

        public function addHilos(Hilo $hilo){
        $this->hilos[] = $hilo;
        return $this;
    }

    public function getHilos(){
        return $this->hilos;
    }
/**
 * TematicaSoporte
 *
 * @ORM\Table(name="tematica_soporte", indexes={@ORM\Index(name="fk_tematica_soporte_personal1_idx", columns={"personal_email"})})
 * @ORM\Entity(repositoryClass="GuiasDocentes\AppBundle\Entity\TematicaSoporteRepository")
 */
class TematicaSoporte
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="enunciado", type="text", nullable=true)
     */
    private $enunciado;

    /**
     * @var integer
     *
     * @ORM\Column(name="orden", type="integer", nullable=false)
     */
    private $orden;

    /**
     * @var \Personal
     *
     * @ORM\ManyToOne(targetEntity="Personal", inversedBy="tematicasSoporte", cascade={"ALL"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="personal_email", referencedColumnName="email")
     * })
     */
    private $personalEmail;
/**
 * Hilo
 *
 * @ORM\Table(name="hilo", indexes={@ORM\Index(name="fk_Hilo_Personal1_idx", columns={"personalEmail"}), @ORM\Index(name="fk_Hilo_Consultante1_idx", columns={"consultanteEmail"})})
 * @ORM\Entity
 */
class Hilo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \GuiasDocentes\AppBundle\Entity\Personal
     *
     * @ORM\ManyToOne(targetEntity="Personal", inversedBy="hilos", cascade ={"ALL"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="personalEmail", referencedColumnName="email")
     * })
     */
    private $personalemail;

    /**
     * @var \GuiasDocentes\AppBundle\Entity\Consultante
     *
     * @ORM\ManyToOne(targetEntity="Consultante", inversedBy="hilos", cascade ={"ALL"} )
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="consultanteEmail", referencedColumnName="email")
     * })
     */
    private $consultanteemail;


    /* Customized code */

    /**
     * @ORM\OneToMany(targetEntity="GuiasDocentes\AppBundle\Entity\Consulta", mappedBy="hiloid")
     * @Assert\Valid()
     */
    private $consultas;

    public function __construct(){

        $this->consultas = new ArrayCollection();
    }

    public function setConsultas (Consulta $consulta){
        $this->hilos[]=$consulta;
    }

    public function addConsulta (\GuiasDocentes\AppBundle\Entity\Consulta $consulta){
        $this->hilos[] = $consulta;
    }


/* End customized code */

与您在此过程中看到的方式相同,我获得了使用Personal类的电子邮件生成的选择字段,需要显示Tematica_soporte类的Enunciado。 Thax

1 个答案:

答案 0 :(得分:0)

以这种方式在TematicaSoporte中添加方法就足够了:

public function getEnunciado(){ return $this->getTematicasSoporte()[0]->getEnunciado(); }