Symfony 3 NoSuchPropertyException

时间:2018-04-20 11:29:57

标签: php symfony doctrine-orm web-deployment

大家好我正在研究一个symfony 3.3项目,我有一个错误我来不解决它,即使我在这里搜索类似的帖子,但我找不到任何适合我的解决方案。 我有两个实体“Cours”和“Categorie”(法语)这两个实体有很多关联。问题是,当我转到创建新Cours的页面时,我收到此错误

  

属性“类别”也不是其中一种方法   “getCategories()”,“categories()”,“isCategories()”,   “hasCategories()”,“__ get()”存在并且在课堂上具有公共访问权限   “LearnBundle \实体\ Categorie”。

这是Cours.php代码

    <?php
namespace LearnBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Cours
 *
 * @ORM\Table(name="cours")
 * @ORM\Entity(repositoryClass="LearnBundle\Repository\CoursRepository")
 */
class Cours
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="titre", type="string", length=255, unique=true)
     * @Assert\NotBlank()
     */
    private $titre;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $image;

    /**
     * @var string
     *
     * @ORM\Column(name="video_intro", type="string", length=255, unique=true)
     * @Assert\NotBlank()
     */
    private $videoIntro;

    /**
     * @var string
     *
     * @ORM\Column(name="serie_videos", type="string", length=255, unique=true)
     * @Assert\NotBlank()
     */
    private $serieVideos;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     * @Assert\NotBlank()
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="prerequis", type="text")
     * @Assert\NotBlank()
     */
    private $prerequis;

    /**
     * @var string
     *
     * @ORM\Column(name="message", type="text")
     * @Assert\NotBlank()
     */
    private $message;

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

    /**
     * @ORM\ManyToMany(targetEntity="LearnBundle\Entity\Categorie", inversedBy="cours")
     * @ORM\JoinTable(name="cours_categories")
     */
    private $categories;

    /**
     * @ORM\OneToMany(targetEntity="Fichier", mappedBy="cours")
     * @Assert\Type(type="AppBundle\Entity\Fichier")
     * @Assert\Valid()
     */
    private $fichier;

    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="cours")
     * @ORM\JoinTable(name="cours_etudiants")
     */
    private $etudiants;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="cours")
     * @ORM\JoinColumn(name="formateur", referencedColumnName="id")
     */
    private $formateur;




    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set titre
     *
     * @param string $titre
     *
     * @return Cours
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

    /**
     * Get titre
     *
     * @return string
     */
    public function getTitre()
    {
        return $this->titre;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Cours
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set videoIntro
     *
     * @param string $videoIntro
     *
     * @return Cours
     */
    public function setVideoIntro($videoIntro)
    {
        $this->videoIntro = $videoIntro;

        return $this;
    }

    /**
     * Get videoIntro
     *
     * @return string
     */
    public function getVideoIntro()
    {
        return $this->videoIntro;
    }

    /**
     * Set serieVideos
     *
     * @param string $serieVideos
     *
     * @return Cours
     */
    public function setSerieVideos($serieVideos)
    {
        $this->serieVideos = $serieVideos;

        return $this;
    }

    /**
     * Get serieVideos
     *
     * @return string
     */
    public function getSerieVideos()
    {
        return $this->serieVideos;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Cours
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set prerequis
     *
     * @param string $prerequis
     *
     * @return Cours
     */
    public function setPrerequis($prerequis)
    {
        $this->prerequis = $prerequis;

        return $this;
    }

    /**
     * Get prerequis
     *
     * @return string
     */
    public function getPrerequis()
    {
        return $this->prerequis;
    }

    /**
     * Set message
     *
     * @param string $message
     *
     * @return Cours
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Get message
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->categories = new ArrayCollection();
        $this->fichier= new ArrayCollection();
    }

    /**
     * Add category
     *
     * @param \LearnBundle\Entity\Categorie $category
     *
     * @return Cours
     */
    public function addCategory(\LearnBundle\Entity\Categorie $category)
    {
        $this->categories[] = $category;

        return $this;
    }

    /**
     * Remove category
     *
     * @param \LearnBundle\Entity\Categorie $category
     */
    public function removeCategory(\LearnBundle\Entity\Categorie $category)
    {
        $this->categories->removeElement($category);
    }

    /**
     * Get categories
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCategories()
    {
        return $this->categories;
    }





    /**
     * Add etudiant
     *
     * @param \LearnBundle\Entity\User $etudiant
     *
     * @return Cours
     */
    public function addEtudiant(\LearnBundle\Entity\User $etudiant)
    {
        $this->etudiants[] = $etudiant;

        return $this;
    }

    /**
     * Remove etudiant
     *
     * @param \LearnBundle\Entity\User $etudiant
     */
    public function removeEtudiant(\LearnBundle\Entity\User $etudiant)
    {
        $this->etudiants->removeElement($etudiant);
    }

    /**
     * Get etudiants
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getEtudiants()
    {
        return $this->etudiants;
    }

    /**
     * Set etat
     *
     * @param string $etat
     *
     * @return Cours
     */
    public function setEtat($etat)
    {
        $this->etat = $etat;

        return $this;
    }

    /**
     * Get etat
     *
     * @return string
     */
    public function getEtat()
    {
        return $this->etat;
    }

    /**
     * Set formateur
     *
     * @param \LearnBundle\Entity\User $formateur
     *
     * @return Cours
     */
    public function setFormateur(\LearnBundle\Entity\User $formateur = null)
    {
        $this->formateur = $formateur;

        return $this;
    }

    /**
     * Get formateur
     *
     * @return \LearnBundle\Entity\User
     */
    public function getFormateur()
    {
        return $this->formateur;
    }

    /**
     * Add fichier
     *
     * @param \LearnBundle\Entity\Fichier $fichier
     *
     * @return Cours
     */
    public function addFichier(\LearnBundle\Entity\Fichier $fichier)
    {
        $this->fichier[] = $fichier;

        return $this;
    }

    /**
     * Remove fichier
     *
     * @param \LearnBundle\Entity\Fichier $fichier
     */
    public function removeFichier(\LearnBundle\Entity\Fichier $fichier)
    {
        $this->fichier->removeElement($fichier);
    }

    /**
     * Get fichier
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getFichier()
    {
        return $this->fichier;
    }


}

这是CoursType.php代码

    <?php

namespace LearnBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use LearnBundle\Entity\Cours;

class CoursType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('titre')
        ->add('image', FileType::class, array('data_class' => null))                                                                                       
        ->add('videoIntro')
        ->add('serieVideos')
        ->add('description')
        ->add('prerequis')
        ->add('message')
        ->add('categories',EntityType::class, array(
                'multiple' => 'true',
                'class' => 'LearnBundle:Categorie',
                'choice_label' => 'categories'


                )

            )
        ->add('Fichier', CollectionType::class, array(
            'entry_type' => FichierType::class,
            'entry_options' => array('label' => false),
        ))

        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'LearnBundle\Entity\Cours'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'learnbundle_cours';
    }


}

这是Categorie.php代码

    <?php

namespace LearnBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Categorie
 *
 * @ORM\Table(name="categorie")
 * @ORM\Entity(repositoryClass="LearnBundle\Repository\CategorieRepository")
 */
class Categorie
{
    /**
     * @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, unique=true)
     */
    private $nom;

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

    /**
     * @ORM\ManyToMany(targetEntity="\LearnBundle\Entity\Cours", mappedBy="categories")
     */
    private $cours;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nom
     *
     * @param string $nom
     *
     * @return Categorie
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Categorie
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->cours = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add cours
     *
     * @param \LearnBundle\Entity\Cours $cour
     *
     * @return Categorie
     */
    public function addCours(\LearnBundle\Entity\Cours $cour)
    {
        $this->cours[] = $cour;

        return $this;
    }

    /**
     * Remove cours
     *
     * @param \LearnBundle\Entity\Cours $cour
     */
    public function removeCours(\LearnBundle\Entity\Cours $cour)
    {
        $this->cours->removeElement($cour);
    }

    /**
     * Get cours
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCours()
    {
        return $this->cours;
    }
}

这是CoursController.php

中newAction函数的代码
public function newAction(Request $request)
    {
        $cour = new Cours();
        $cour->setEtat('en_attente');
        $form = $this->createForm('LearnBundle\Form\CoursType', $cour);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            //traitement d'image de cours
            $image = $cour->getImage();
            $imageName = md5(uniqid()).'.'.$image->guessExtension();
            $image->move($this->getParameter('images_directory'),$imageName);
            $cour->setImage($imageName);
            //enregistrement de cours

            $em->persist($cour);
            $em->flush();

            return $this->redirectToRoute('cours_show', array('id' => $cour->getId()));
        }

    return $this->render('cours/new.html.twig', array(
        'cour' => $cour,
        'form' => $form->createView(),
    ));
}

1 个答案:

答案 0 :(得分:0)

更改choice_label'=&gt; '类别' choice_label'=&gt; 'NOM'