Symfony 3.2 CollectionType

自闭症网瘾萝莉.ら 提交于 2020-02-08 02:28:06

问题


Here's my problem. In my project, I have a one-to-many relationship between class FactureAchat and LigneFactureAchat, when I add a Facture the products are added in the Table ligne_facture_achat without having adding the foreign key of my Facture and an error is produced "Could not determine access type for property "LinesInvoicesPurchases". " the same problem with the display of a facture with its products. "An exception has been thrown during the rendering of a template (" Notice: Undefined index: factureachat ")."

FactureAchat Entity

 /*----- 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);
    }

LigneFactureAchat Entity

 /**
     * @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;
    }

FactureAchat Form

$builder->add('lignesFacturesAchats', CollectionType::class, array(
            'entry_type' => LigneFactureAchatType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'mapped' => true,
            'by_reference' => false
        ));

FactureAchatController

/**
     * 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 View

<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>

the error "An exception has been thrown during the rendering of a template (" Notice: Undefined index: factureachat ")." occurred on line

{% for ligne in factureachat.lignesFacturesAchats %}

Any help please

来源:https://stackoverflow.com/questions/60109999/symfony-3-2-collectiontype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!