在属性路径src \ Entity \中给定的src \ Entity \ ...类型的预期参数

时间:2019-06-02 03:16:45

标签: forms symfony

我尝试做一个嵌入式表格并且有两个错误: 1-当我单击添加时添加体验:我获得2个子表格体验 2-当我在添加体验后进行保存时,出现此错误-在属性路径“体验”中给出的类型为“ src \ Entity \ Experience”,“ App \ Entity \ Experience”的预期参数。---

enter image description here

    <blink>
//controller
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request ;

use App\Form\PersonneType;
use App\Entity\Personne;
use App\Entity\Experience;

//

use App\Repository\ExperienceRepository;

class BienvenuController extends AbstractController
{
    /**
     * @Route("/bienvenu", name="bienvenu")
     */
    public function index(Request $request  )
    {

        $personne =New Personne ();


                // dummy code - this is here just so that the Task has some tags
        // otherwise, this isn't an interesting example
        $experience1 = new Experience ();

        $experience1 -> setTitre ( 'titre1' );
        $experience1 -> setLocation ( 'location1' );
        $experience1 -> setPersonne ( $personne );
        $personne -> getExperiences () -> add ( $experience1 );

        $experience2 = new Experience ();
        $experience2 -> setTitre ( 'titre2' );
        $experience2 -> setLocation ( 'location2' );
        $experience2 -> setPersonne ( $personne );          //pour ajouter l id du personne a la table = emtity experience
        $personne -> getExperiences () -> add ( $experience2 );
        // end dummy code

        $form = $this -> createForm ( PersonneType :: class , $personne );

        $form -> handleRequest ( $request );

        if ($form->isSubmitted() && $form->isValid()) {
            // ... maybe do some form processing, like saving the Task and Tag objects

            $personne = $form->getData();
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($personne);
            $entityManager->flush();

            return $this->render('base.html.twig', [

            ]);

        }

        return $this->render('bienvenu/index.html.twig', [
            'form' => $form -> createView ()
        ]);
    }
}
    </blink>

    <blink>
//Experience Entity
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ExperienceRepository")
 */
class Experience
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $titre;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $location;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Personne", inversedBy="experiences")
     */
    private $personne;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitre(): ?string
    {
        return $this->titre;
    }

    public function setTitre(string $titre): self
    {
        $this->titre = $titre;

        return $this;
    }

    public function getLocation(): ?string
    {
        return $this->location;
    }

    public function setLocation(string $location): self
    {
        $this->location = $location;

        return $this;
    }

    public function getPersonne(): ?Personne
    {
        return $this->personne;
    }

    public function setPersonne(?Personne $personne): self
    {
        $this->personne = $personne;

        return $this;
    }
}

    </blink>

    <blink>
//Personne Entity
<?php

namespace App\Form;

use App\Form\ExperienceType;
use App\Entity\Personne;
use Symfony\Component\Form\Extension\Core\Type\CollectionType ;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Form\Extension\Core\Type\SubmitType ;


class PersonneType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Nom')
            ->add('age')
            ->add('save', SubmitType::class, [
                'attr' => ['class' => 'save']])
            ->add('experiences', CollectionType::class,[
                'entry_type' => ExperienceType :: class ,
                'entry_options' => [ 'label' => false ],
                'allow_add' => true ,
                'allow_delete' => true ,
                'by_reference' => false ,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Personne::class,
        ]);
    }

}

    </blink>

    <blink>
//index.html.twig
{% extends 'base.html.twig' %}

{% block title %}Hello BienvenuController!
{% endblock %}

    {% block stylesheets %}

        <script src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
        </script>

    {% endblock %}


{% block body %}

    <div class="example-wrapper">
        {{ form_start(form) }}
        {{ form_row(form.Nom) }}
        {{ form_row(form.age) }}
        ------------
        <ul class="experiences" data-prototype=" {{ form_widget ( form.experiences.vars.prototype )| e ( 'html_attr' ) }} ">
            {# iterate over each existing experience and render its only field: name #}
            {% for experience in form.experiences %}
            <li>{{ form_row(experience.titre) }}</li>
            <li>{{ form_row(experience.location) }}</li>
            {% endfor %}
        </ul>

        {{ form_end(form) }}
    </div>

    {% block javascripts %} 
        <script>
            var $collectionHolder;
            // setup an "add a tag" link
            var $addExperienceButton = $('<button type="button" class="add_experience_link">Add experience</button>');
            var $newLinkLi = $('<li></li>').append($addExperienceButton);

            jQuery(document).ready(function () {
                // Get the ul that holds the collection of experiences
                $collectionHolder = $('ul.experiences');

                $collectionHolder.append($newLinkLi);

                // count the current form inputs we have (e.g. 2), use that as the new
                // index when inserting a new item (e.g. 2)
                $collectionHolder.data('index', $collectionHolder.find(':input').length);

                $addExperienceButton.on('click', function (e) {
                    // add a new tag form (see next code block)
                    addTagForm($collectionHolder, $newLinkLi);
                });
            });
            //*************************************//
            function addTagForm ( $collectionHolder , $newLinkLi ) {
                // Get the data-prototype explained earlier
                var prototype = $collectionHolder . data ( 'prototype' );

                // get the new index
                var index = $collectionHolder . data ( 'index' );

                var newForm = prototype ;
                // You need this only if you didn't set 'label' => false in your tags field in TaskType
                // Replace '__name__label__' in the prototype's HTML to
                // instead be a number based on how many items we have
                // newForm = newForm.replace(/__name__label__/g, index);

                // Replace '__name__' in the prototype's HTML to
                // instead be a number based on how many items we have
//                newForm = newForm . replace ( /__name__/g , index );

                // increase the index with one for the next item
                $collectionHolder . data ( 'index' , index + 1 );

                // Display the form in the page in an li, before the "Add a tag" link li
                var $newFormLi = $ ( '<li></li>' ). append ( newForm );
                $newLinkLi . before ( $newFormLi );

                //////// **********delete ************* /////////////////
                                    /////*******/////////////////////////

                    // add a delete link to the new form
                    addTagFormDeleteLink ( $newFormLi );
                    /////*******/////////////////////////
                    function addTagFormDeleteLink ( $tagFormLi ) {
                        var $removeFormButton = $ ( '<button type="button">Delete this experience</button>' );
                        $tagFormLi . append ( $removeFormButton );

                        $removeFormButton . on ( 'click' , function ( e ) {
                            // remove the li for the tag form
                            $tagFormLi . remove ();
                        });
                    }
                    /////*******/////////////////////////

            }
        </script>
    {% endblock %}
{% endblock %}
    </blink>

    <blink>
//Experience Type
<?php

namespace App\Form;

use App\Entity\Experience;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Form\Extension\Core\Type\HiddenType;

class ExperienceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('titre')
            ->add('location')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Experience::class,
        ]);
    }
}

    </blink>


    <blink>
//Personne Type
<?php

namespace App\Form;

use App\Form\ExperienceType;
use App\Entity\Personne;
use Symfony\Component\Form\Extension\Core\Type\CollectionType ;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Form\Extension\Core\Type\SubmitType ;


class PersonneType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Nom')
            ->add('age')
            ->add('save', SubmitType::class, [
                'attr' => ['class' => 'save']])
            ->add('experiences', CollectionType::class,[
                'entry_type' => ExperienceType :: class ,
                'entry_options' => [ 'label' => false ],
                'allow_add' => true ,
                'allow_delete' => true ,
                'by_reference' => false ,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Personne::class,
        ]);
    }

}

    </blink>

1 个答案:

答案 0 :(得分:1)

问题可能出在第9行的https://pastebin.com/e5s2VAJCPersonne实体)上,而不是use src\Entity\Experience;上,您应该使用use App\Entity\Experience;