问题
I'm trying to create a form based on my related entities in doctrine by Symfony 2. I have different entities: Tematica, Personal, Hilo, Consultante, Consulta, Consulta_Asignatura and Asignatura. I reach to persist all my data, nevertheless have some problems with entities Hilo Personal and Tematica. In my form need to show select imput, it should have all "Enunciado" rows and later recover Email of selected option in this select input. I'll share my code, I hope somebody can help my.
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 */
In the same way that you can see with this process I get select field generated with email of Personal class y need to show Enunciado of Tematica_soporte class. Thax
回答1:
Was enough with add a method inside TematicaSoporte in this way:
public function getEnunciado(){ return $this->getTematicasSoporte()[0]->getEnunciado(); }
来源:https://stackoverflow.com/questions/37185149/recover-values-select-imput-related-entities-doctrine-symfony-2