我对Symfony有问题,我正在尝试动态嵌套表单
有一个订单,每个订单有多张票证
当我坚持表格时,我有一张桌子,上面有票证和订单,但是没有commands_id,所以没有链接
这是我的代码:
CommandesType.php
namespace CoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class CommandesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dateCommande', DateType::class)
->add('email', TextType::class)
->add('typeCmd', TextType::class)
->add('totalPrice', TextType::class)
->add('dateVisite', DateType::class)
->add('billets', CollectionType::class, array(
'entry_type' => BilletsType::class,
'allow_add' => true,
'allow_delete' => true
))
->add('save', SubmitType::class);
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CoreBundle\Entity\Commandes'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'corebundle_commandes';
}
}
BilletsType.php
namespace CoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BilletsType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom')->add('prenom')->add('country')->add('dateNaissance')->add('typeBillet')->add('commandes');
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CoreBundle\Entity\Billets'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'corebundle_billets';
}
}
实体Billets.php
namespace CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Billets
*
* @ORM\Table(name="billets")
* @ORM\Entity(repositoryClass="CoreBundle\Repository\BilletsRepository")
*/
class Billets
{
/**
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Commandes", inversedBy="billets", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $commandes;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="prenom", type="string", length=255)
*/
private $prenom;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255)
*/
private $country;
/**
* @var \DateTime
*
* @ORM\Column(name="dateNaissance", type="date")
*/
private $dateNaissance;
/**
* @var int
*
* @ORM\Column(name="typeBillet", type="integer")
*/
private $typeBillet;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*
* @return Billets
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
*
* @return Billets
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set country
*
* @param string $country
*
* @return Billets
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set dateNaissance
*
* @param \DateTime $dateNaissance
*
* @return Billets
*/
public function setDateNaissance($dateNaissance)
{
$this->dateNaissance = $dateNaissance;
return $this;
}
/**
* Get dateNaissance
*
* @return \DateTime
*/
public function getDateNaissance()
{
return $this->dateNaissance;
}
/**
* Set typeBillet
*
* @param integer $typeBillet
*
* @return Billets
*/
public function setTypeBillet($typeBillet)
{
$this->typeBillet = $typeBillet;
return $this;
}
/**
* Get typeBillet
*
* @return int
*/
public function getTypeBillet()
{
return $this->typeBillet;
}
/**
* Set commandes
*
* @param \CoreBundle\Entity\Commandes $commandes
*
* @return Billets
*/
public function setCommandes(\CoreBundle\Entity\Commandes $commandes)
{
$this->commandes = $commandes;
return $this;
}
/**
* Get commandes
*
* @return \CoreBundle\Entity\Commandes
*/
public function getCommandes()
{
return $this->commandes;
}
}
Entity Commandes.php
namespace CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Commandes
*
* @ORM\Table(name="commandes")
* @ORM\Entity(repositoryClass="CoreBundle\Repository\CommandesRepository")
*/
class Commandes
{
/**
* @ORM\OneToMany(targetEntity="CoreBundle\Entity\Billets", mappedBy="commandes", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $billets;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="dateCommande", type="date")
*/
private $dateCommande;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="typeCmd", type="string", length=255)
*/
private $typeCmd;
/**
* @var int
*
* @ORM\Column(name="totalPrice", type="integer")
*/
private $totalPrice;
/**
* @var \DateTime
*
* @ORM\Column(name="dateVisite", type="date")
*/
private $dateVisite;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set dateCommande
*
* @param \DateTime $dateCommande
*
* @return Commandes
*/
public function setDateCommande($dateCommande)
{
$this->dateCommande = $dateCommande;
return $this;
}
/**
* Get dateCommande
*
* @return \DateTime
*/
public function getDateCommande()
{
return $this->dateCommande;
}
/**
* Set email
*
* @param string $email
*
* @return Commandes
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set typeCmd
*
* @param string $typeCmd
*
* @return Commandes
*/
public function setTypeCmd($typeCmd)
{
$this->typeCmd = $typeCmd;
return $this;
}
/**
* Get typeCmd
*
* @return string
*/
public function getTypeCmd()
{
return $this->typeCmd;
}
/**
* Set totalPrice
*
* @param integer $totalPrice
*
* @return Commandes
*/
public function setTotalPrice($totalPrice)
{
$this->totalPrice = $totalPrice;
return $this;
}
/**
* Get totalPrice
*
* @return int
*/
public function getTotalPrice()
{
return $this->totalPrice;
}
/**
* Set dateVisite
*
* @param \DateTime $dateVisite
*
* @return Commandes
*/
public function setDateVisite($dateVisite)
{
$this->dateVisite = $dateVisite;
return $this;
}
/**
* Get dateVisite
*
* @return \DateTime
*/
public function getDateVisite()
{
return $this->dateVisite;
}
/**
* Constructor
*/
public function __construct()
{
$this->billets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add billet
*
* @param \CoreBundle\Entity\Billets $billet
*
* @return Commandes
*/
public function addBillet(\CoreBundle\Entity\Billets $billet)
{
$this->billets[] = $billet;
return $this;
}
/**
* Remove billet
*
* @param \CoreBundle\Entity\Billets $billet
*/
public function removeBillet(\CoreBundle\Entity\Billets $billet)
{
$this->billets->removeElement($billet);
}
/**
* Get billets
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBillets()
{
return $this->billets;
}
}
还有控制器
namespace CoreBundle\Controller;
use CoreBundle\Entity\Commandes;
use CoreBundle\Form\CommandesType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$commandes = new Commandes();
$form = $this->createForm(CommandesType::class, $commandes);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($commandes);
$em->flush();
return $this->render('@Core/Default/index.html.twig', array(
'form' => $form->createView(),
));
}
}
我已经进行了几次搜索,我认为问题出在控制器或实体上,但是找不到!
谢谢您的帮助
答案 0 :(得分:0)
您的Commandes实体无效。
尝试一下:
/**
* Add billet
*
* @param \CoreBundle\Entity\Billets $billet
*
* @return Commandes
*/
public function addBillet(\CoreBundle\Entity\Billets $billet)
{
$billet->setCommandes($this);
$this->billets->add($billet);
return $this;
}