我有一个Centros.php
定义如下的实体:
<?php
namespace PI\CentroBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="centros")
*/
class Centros {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="text")
*/
protected $descripcion;
public function getId() {
return $this->id;
}
public function setDescripcion($descripcion) {
$this->descripcion = $descripcion;
}
public function getDescripcion() {
return $this->descripcion;
}
}
并且还有另一个UnidadesHasCentros.php
具有此定义的:
<?php
namespace PI\UnidadBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="unidades_has_centros")
*/
class UnidadesHasCentros {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="PI\UnidadBundle\Entity\Unidades")
* @ORM\JoinColumn(name="unidades_id", referencedColumnName="id")
*/
protected $unidades_id;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="PI\CentroBundle\Entity\Centros")
* @ORM\JoinColumn(name="centros_id", referencedColumnName="id")
*/
protected $centros_id;
public function setUnidadesId(\PI\UnidadBundle\Entity\Unidades $unidad_id) {
$this->unidades_id = $unidad_id;
}
public function getUnidadesId() {
return $this->unidades_id;
}
public function setCentroId(\PI\CentroBundle\Entity\Centros $centros_id) {
$this->centros_id = $centros_id;
}
public function getCentroId() {
return $this->centros_id;
}
}
这是我的CentroType.php
表格,你可以看到我没有映射实体:
<?php
namespace PI\CentroBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CentroType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('unidades', 'entity', array('class' => 'PIUnidadBundle:Unidades', 'property' => 'descripcion', 'required' => false, 'multiple' => true, 'expanded' => false, 'mapped' => false, 'attr' => array('class' => 'form-control')))
->add('descripcion', 'text', array('attr' => array('class' => 'form-control')));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'PI\CentroBundle\Entity\Centros'
));
}
public function getName() {
return 'centro';
}
}
当我创建一个新的 Centro 时,我可以毫无问题地分配 Unidades,如下所示:
public function createAction(Request $request) {
$entity = new Centros();
$form = $this->createForm(new CentroType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
if ($request->get('unidades')) {
foreach ($request->get('unidades') as $key => $value) {
$entityUnidadesHasCentros = new UnidadesHasCentros();
$entityUnidadesHasCentros->setCentroId($entity);
$entityUnidadesHasCentros->setUnidadesId($value);
$em->persist($entityUnidadesHasCentros);
}
$em->flush();
}
}
return $this->redirect($this->generateUrl('listar-centros'));
}
但是我如何通过编辑/更新操作来处理这个?我的意思是我需要获取所有关系并将其显示在编辑表单中,但不知道如何操作。如果您有更好的解决方案,请告诉我。