我在Symfony中遇到了一个形式系统问题。
我有两个实体:Annonce和Ville。
One Ville可以有很多annonce,所以这里是实体声明:
Annonce:
**
class Annonce
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Ville",cascade={"persist"})
* @ORM\JoinColumn(name="ville_id", referencedColumnName="id")
*/
private $ville;
**
和Ville:
class Ville
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="ville", type="string", length=255)
*/
private $ville;
/**
* @ORM\OneToMany(targetEntity="Adresse", mappedBy="ville")
*/
我还为Annonce创建了一个FormType,如下所示:
class AnnonceType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('voiture', VoitureType::class)
->add('ville', EntityType::class, array(
'class' => 'AppBundle:Ville',
'placeholder' => ''
))
->add('prixJour', IntegerType::class, array(
'label'=>'Cmimi ditor i qerase'
))
->add('exigences', TextareaType::class, array(
'label'=>'Eksigjenca te tjera'
))
/*
->add('photos', FileType::class, array(
'required'=>false,
'multiple'=>true
))*/
->add('save', SubmitType::class, array(
'label' => 'Ruaj annoncen tende'
));
/*
$builder->get('photos')
->addModelTransformer(new FilesToPhotosTransformer());**/
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Annonce'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_annonce';
}
}
控制器:
public function editAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$annonce = $em->getRepository('AppBundle:Annonce')->find($id);
$form = $this->createForm(AnnonceType::class, $annonce);
if (null === $annonce) {
throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas.");
}
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$session = $request->getSession();
$tabPhotos = $session->get('photos');
if ($tabPhotos) {
foreach ($tabPhotos as $ph) {
$photo = $this->getDoctrine()
->getRepository('AppBundle:Photo')
->find($ph);
$annonce->addPhoto($photo);
};
};
$em->flush();
$session->remove('photos');
$request->getSession()->getFlashBag()->add('notice', 'Annonce bien enregistrée.');
return $this->redirectToRoute('profile');
}
return $this->render('corent/edit_vehicle.html.twig', array('form'=>$form->createView(),"photos"=>$arrayPhoto,"annonceID"=>$annonce->getID()));
}
当我从控制器渲染视图时,Ville字段是正确的,我列出了所有的villes。 但是当我提交表单时,ville提交的数据总是为空,我无法理解为什么。
由于