簡體   English   中英

CollectionType +嵌套表單-按ID連接-Symfony 3.4

[英]CollectionType + nested form - join by id - symfony 3.4

我有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;
        }
    }

實體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(),
                ));




            }
        }

我已經進行了幾次搜索,我認為問題來自控制器或實體,但找不到!

謝謝您的幫助

您有一個無效的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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM