Forms Symfony 3.2收集类型

Forms Symfony 3.2收集类型,forms,entity,one-to-many,arraycollection,symfony-3.2,Forms,Entity,One To Many,Arraycollection,Symfony 3.2,这是我的问题。在我的项目中,我在类FactureAchat和LigneFactureAchat之间有一个一对多的关系,当我添加一个Facture时,产品被添加到表ligne\u Facture\achat中,而没有添加我的Facture的外键,并且产生了一个错误“无法确定属性“LinesInvoicesPurchases”的访问类型”同样的问题也出现在产品的展示上。“在呈现模板期间引发了异常(“注意:未定义索引:factureachat”)。” 制造实体 /*----- added from

这是我的问题。在我的项目中,我在类FactureAchat和LigneFactureAchat之间有一个一对多的关系,当我添加一个Facture时,产品被添加到表ligne\u Facture\achat中,而没有添加我的Facture的外键,并且产生了一个错误“无法确定属性“LinesInvoicesPurchases”的访问类型”同样的问题也出现在产品的展示上。“在呈现模板期间引发了异常(“注意:未定义索引:factureachat”)。”

制造实体

 /*----- added from facture---*/
    /**
     * @ORM\OneToMany(targetEntity="LigneFactureAchat", mappedBy="factureachat",cascade={"all"})
     * @Assert\Valid()
     */
    protected $lignesFacturesAchats;

    public function __construct() {
        $this->lignesFacturesAchats = new ArrayCollection();
        $this->dateCreation = new \DateTime();
        $this->dateEcheance = new \DateTime();
    }

    /**
     * Get lignesFacturesAchats
     *
     * @return \AppBundle\Entity\LigneFactureAchat
     */
    public function getLignesFacturesAchats() {
        return $this->lignesFacturesAchats;
    }



    public function addLignesFactureAchat(LigneFactureAchat $l) {
        $l->setFactureAchat($this);
        $this->lignesFacturesAchats->add($l);
    }

    public function removeLignesFactureAchat(LigneFactureAchat $l) {
        $this->lignesFacturesAchats->removeElement($l);
    }
 /**
     * @var \FactureAchat
     *
     * @ORM\ManyToOne(targetEntity="FactureAchat",inversedBy="lignesFacturesAchats",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="facture_achat_id", referencedColumnName="id")
     * })
     */
    private $factureAchat;


    /**
     * Set factureAchat.
     *
     * @param \AppBundle\Entity\FactureAchat|null $factureAchat
     *
     * @return LigneFactureAchat
     */
    public function setFactureAchat(\AppBundle\Entity\FactureAchat $factureAchat = null)
    {
        $this->factureAchat = $factureAchat;

        return $this;
    }

    /**
     * Get factureAchat.
     *
     * @return \AppBundle\Entity\FactureAchat|null
     */
    public function getFactureAchat()
    {
        return $this->factureAchat;
    }
LigneFactureAchat实体

 /*----- added from facture---*/
    /**
     * @ORM\OneToMany(targetEntity="LigneFactureAchat", mappedBy="factureachat",cascade={"all"})
     * @Assert\Valid()
     */
    protected $lignesFacturesAchats;

    public function __construct() {
        $this->lignesFacturesAchats = new ArrayCollection();
        $this->dateCreation = new \DateTime();
        $this->dateEcheance = new \DateTime();
    }

    /**
     * Get lignesFacturesAchats
     *
     * @return \AppBundle\Entity\LigneFactureAchat
     */
    public function getLignesFacturesAchats() {
        return $this->lignesFacturesAchats;
    }



    public function addLignesFactureAchat(LigneFactureAchat $l) {
        $l->setFactureAchat($this);
        $this->lignesFacturesAchats->add($l);
    }

    public function removeLignesFactureAchat(LigneFactureAchat $l) {
        $this->lignesFacturesAchats->removeElement($l);
    }
 /**
     * @var \FactureAchat
     *
     * @ORM\ManyToOne(targetEntity="FactureAchat",inversedBy="lignesFacturesAchats",cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="facture_achat_id", referencedColumnName="id")
     * })
     */
    private $factureAchat;


    /**
     * Set factureAchat.
     *
     * @param \AppBundle\Entity\FactureAchat|null $factureAchat
     *
     * @return LigneFactureAchat
     */
    public function setFactureAchat(\AppBundle\Entity\FactureAchat $factureAchat = null)
    {
        $this->factureAchat = $factureAchat;

        return $this;
    }

    /**
     * Get factureAchat.
     *
     * @return \AppBundle\Entity\FactureAchat|null
     */
    public function getFactureAchat()
    {
        return $this->factureAchat;
    }
制成品表

$builder->add('lignesFacturesAchats', CollectionType::class, array(
            'entry_type' => LigneFactureAchatType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'mapped' => true,
            'by_reference' => false
        ));
工厂自动控制器

/**
     * Creates a new factureachat entity.
     *
     * @Route("/new", name="factureachat_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request) {
        $em = $this->getDoctrine()->getManager();
        $retenus = $em->getRepository('AppBundle:Retenu')->findAll();
        $factureachat = new FactureAchat();
        $form = $this->createForm('AppBundle\Form\FactureAchatType', $factureachat);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($factureachat);
            $em->flush($factureachat);
            // var_dump($factureachat->getLignesFacturesAchats());die;
            if ($form->get('saveAndPrint')->isClicked()) {
                return $this->redirectToRoute('factureachat_print', array('id' => $factureachat->getId()));
            }
            // return $this->redirectToRoute('factureachat_show', array('id' => $factureachat->getId()));
        }
        return $this->render('factureachat/new.html.twig', array(
                    'factureachat' => $factureachat,
                    'form' => $form->createView(),
                    'retenus' => $retenus
        ));
    }

    /**
     * @Route("/{id}/show",name="factureachat_show")
     * @Method({"GET","POST"})
     */
    public function showAction(Request $request, FactureAchat $factureachat) {
        $form_regler = $this->createFormBuilder($factureachat)
                ->add('termine', \Symfony\Component\Form\Extension\Core\Type\HiddenType::class, array(
                    'data' => true
                ))
                ->add('terminerAndRegler', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array('label' => 'Terminer la facture et régler', 'attr' => ['class' => 'btn-success']))
                ->getForm();
        $form_imprimer = $this->createFormBuilder($factureachat)
                ->add('termine', \Symfony\Component\Form\Extension\Core\Type\HiddenType::class, array(
                    'data' => true
                ))
                ->add('terminerAndImprimer', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array('label' => 'Terminer la facture et imprimer', 'attr' => ['class' => 'btn-success']))
                ->getForm();
        $form_regler->handleRequest($request);
        $form_imprimer->handleRequest($request);
        if ($form_regler->isSubmitted() && $form_regler->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->flush();
            return $this->redirectToRoute('factureachat_reglements', array('id' => $factureachat->getId()));
        }
        if ($form_imprimer->isSubmitted() && $form_imprimer->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->flush();
            return $this->redirectToRoute('factureachat_print', array('id' => $factureachat->getId()));
        }
        return $this->render('factureachat/show.html.twig', array(
                    'factureachat' => $factureachat,
                    'form_regler' => $form_regler->createView(),
                    'form_imprimer' => $form_imprimer->createView(),
                    'lignesFacturesAchats' => $factureachat->getLignesFacturesAchats()

        ));
    }
show.html.twig视图

<div id="collapseTwo" class="panel-collapse collapse in">
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-hover">
                                            <thead>
                                                <tr>
                                                    <th>Réf</th>
                                                    <th>Désignation</th>
                                                    <th>Unité</th>
                                                    <th>PU HT</th>
                                                    <th>Remise</th>
                                                    <th>TVA</th>
                                                    <th>PU TTC</th>
                                                    <th>Qte</th>
                                                    <th>Total TTC</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% set i=0 %}
                                                {% for ligne in factureachat.lignesFacturesAchats %}
                                                    <tr class="item">
                                                        <td class="left">{{ligne.article.code}}</td>
                                                        <td class="left">{{ligne.designation}}</td>
                                                        <td class="left">{{ligne.article.unite}}</td>
                                                        <td class="center" id="prixUnitaire_{{i}}">{{ligne.prixUnitaire}}</td>

                                                        <td class="center" id="remise_{{i}}">{{ligne.remise}} %</td>

                                                        <td class="center" id="tva_{{i}}">{{ligne.tva }} %</td>
                                                        {% set puTTC=ligne.ttc/ligne.qte %}
                                                        <td class="center" id="ttc_{{i}}">{{ puTTC|number_format(3, '.', '') }}</td>
                                                        <td class="center" id="qte_{{i}}">{{ligne.qte}}</td>
                                                        <td class="center" id="total_{{i}}">{{ligne.ttc }}</td>
                                                    </tr>
                                                    {% set i=i+1 %}
                                                {% endfor %}
                                            <div id="lignesFacturesLength" style="visibility: hidden">{{i}}</div>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

请提供任何帮助

FK说,它的值在别处显示为PK。因此,首先创建引用行,然后创建行项目行。这当然是一个常见问题。在考虑发帖之前,请始终用谷歌搜索任何错误消息或你的问题/问题/目标的许多清晰、简洁和准确的措辞,有或没有你的特定字符串/名称和网站:stackoverflow.com&tags,并阅读许多答案。如果你发布一个问题,用一句话作为标题。请参阅文本上方的投票箭头鼠标(&T)。此代码是否显示问题的最小值?这是否限制为尽可能少的层?请在代码问题中给出一个--cut&paste&runnable代码;示例输入(作为初始化代码)以及所需和实际输出(包括逐字错误消息);标签和版本;清晰的说明和解释。对于包含最少代码的错误,您可以给出“显示为OK”的代码,并通过“显示为not OK”的代码进行扩展。(调试基础。)