我正试图根据Symfony 2原理中的相关实体创建一个表单。我有不同的实体:Tematica、Personal、Hilo、Consultante、Consulta、Consulta_Signatura和Asignatura。我坚持我的所有数据,然而与实体Hilo Personal和Tematica有一些问题。在我的表单中,需要显示选择输入,它应该有所有的"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' => 'GuiasDocentesAppBundleEntityPersonal',
'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
*
* @ORMTable(name="personal")
* @ORMEntity(repositoryClass="GuiasDocentesAppBundleEntityPersonalRepository")
class Personal
{
/**
* @var string
*
* @ORMColumn(name="email", type="string", length=50, nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $email;
/**
* @var string
*
* @ORMColumn(name="nombre", type="string", length=30, nullable=true)
*/
private $nombre;
/**
* @var string
*
* @ORMColumn(name="apellidos", type="string", length=50, nullable=true)
*/
private $apellidos;
/**
* @var string
*
* @ORMColumn(name="departamento", type="string", length=20, nullable=true)
*/
private $departamento;
/* Customized code */
/**
* @ORMOneToMany(targetEntity="GuiasDocentesAppBundleEntityTematicaSoporte", mappedBy="personalEmail")
* @AssertValid()
*/
private $tematicasSoporte;
public function __constructor(){
$this->tematicasSoporte = new ArrayCollection();
$this->hilos = new ArrayCollection();
}
public function addTematicasSoporte(GuiasDocentesAppBundleEntityTematicaSoporte $tematicaSoporte){
$this->tematicasSoporte[] = $tematicaSoporte;
return $this;
}
public function setTematicasSoporte(GuiasDocentesAppBundleEntityTematicaSoporte $tematicaSoporte){
$this->tematicasSoporte[] = $tematicaSoporte;
return $this;
}
public function getTematicasSoporte(){
return $this->tematicasSoporte;
}
/**
* @ORMOneToMany(targetEntity="GuiasDocentesAppBundleEntityHilo", mappedBy="personalemail")
* @AssertValid()
*/
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
*
* @ORMTable(name="tematica_soporte", indexes={@ORMIndex(name="fk_tematica_soporte_personal1_idx", columns={"personal_email"})})
* @ORMEntity(repositoryClass="GuiasDocentesAppBundleEntityTematicaSoporteRepository")
*/
class TematicaSoporte
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="enunciado", type="text", nullable=true)
*/
private $enunciado;
/**
* @var integer
*
* @ORMColumn(name="orden", type="integer", nullable=false)
*/
private $orden;
/**
* @var Personal
*
* @ORMManyToOne(targetEntity="Personal", inversedBy="tematicasSoporte", cascade={"ALL"})
* @ORMJoinColumns({
* @ORMJoinColumn(name="personal_email", referencedColumnName="email")
* })
*/
private $personalEmail;
/**
* Hilo
*
* @ORMTable(name="hilo", indexes={@ORMIndex(name="fk_Hilo_Personal1_idx", columns={"personalEmail"}), @ORMIndex(name="fk_Hilo_Consultante1_idx", columns={"consultanteEmail"})})
* @ORMEntity
*/
class Hilo
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var GuiasDocentesAppBundleEntityPersonal
*
* @ORMManyToOne(targetEntity="Personal", inversedBy="hilos", cascade ={"ALL"})
* @ORMJoinColumns({
* @ORMJoinColumn(name="personalEmail", referencedColumnName="email")
* })
*/
private $personalemail;
/**
* @var GuiasDocentesAppBundleEntityConsultante
*
* @ORMManyToOne(targetEntity="Consultante", inversedBy="hilos", cascade ={"ALL"} )
* @ORMJoinColumns({
* @ORMJoinColumn(name="consultanteEmail", referencedColumnName="email")
* })
*/
private $consultanteemail;
/* Customized code */
/**
* @ORMOneToMany(targetEntity="GuiasDocentesAppBundleEntityConsulta", mappedBy="hiloid")
* @AssertValid()
*/
private $consultas;
public function __construct(){
$this->consultas = new ArrayCollection();
}
public function setConsultas (Consulta $consulta){
$this->hilos[]=$consulta;
}
public function addConsulta (GuiasDocentesAppBundleEntityConsulta $consulta){
$this->hilos[] = $consulta;
}
/* End customized code */
正如你在这个过程中看到的那样,我得到了用Personal类的电子邮件生成的select字段,y需要显示Tematic_oporte类的Enunciado。Thax
在TematicSoporte中以这种方式添加一个方法就足够了:
public function getEnunciado(){ return $this->getTematicasSoporte()[0]->getEnunciado(); }