且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

symfony2表单中找不到实体错误

更新时间:2022-10-15 10:16:07

尝试使用 \ 在实体命名空间前面:

 'class'=>'\Evr\HomeBundle\ Entity\Language',


I have a symfony2 Form CategoryType with a buildForm:

  public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('language', 'entity', array(
                    'class' => 'Evr\HomeBundle\Entity\Language',
                    'property' => 'language'
                        )
                )
                ->add('category', 'text', array('label' => 'category.category', 'required' => true));
    }

As you can expect, I have two entities Category and Language, of which Category is a child of Language (One language can have many categories, and one category belongs to 1 or 0 language)

Category

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 *
 * @ORM\Table(name="ev_category")
 * @ORM\Entity
 */
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="category_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

     /**
     * 
     * @ORM\ManyToOne(targetEntity="Language",inversedBy="categories")
     * @ORM\JoinColumn(name="language_id",referencedColumnName="language_id")
     */
    private $language;

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

    /**
     * @ORM\OneToMany(targetEntity="Subcategory", mappedBy="category")
     */
    protected $subcategories;

    public function __construct(){
        $this->subcategories=new ArrayCollection();
    }

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

       /**
     * Set language
     *
     * @param integer $language
     * @return Category
     */
    public function setLanguage($language) {
        $this->language = $language;

        return $this;
    }

    /**
     * Get language
     *
     * @return integer 
     */
    public function getLanguage() {
        return $this->language;
    }

    /**
     * Set category
     *
     * @param \string $category
     * @return Category
     */
    public function setCategory($category)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \string 
     */
    public function getCategory()
    {
        return $this->category;
    }

}

Language

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Language
 *
 * @ORM\Table(name="ev_language")
 * @ORM\Entity
 */
class Language
{
    /**
     * @var integer
     *
     * @ORM\Column(name="language_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @var boolean
     *
     * @ORM\Column(name="direction", type="boolean")
     */
    private $direction;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="language")
     */
    protected $categories;

     /**
     * @ORM\OneToMany(targetEntity="Country", mappedBy="language")
     */
    protected $countries;

    public function __construct(){
        $this->categories=new ArrayCollection();
        $this->countries=new ArrayCollection();
    }

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

    /**
     * Set language
     *
     * @param string $language
     * @return Language
     */
    public function setLanguage($language)
    {
        $this->language = $language;

        return $this;
    }

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

    /**
     * Set code
     *
     * @param string $code
     * @return Language
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

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

    /**
     * Set direction
     *
     * @param boolean $direction
     * @return Language
     */
    public function setDirection($direction)
    {
        $this->direction = $direction;

        return $this;
    }

    /**
     * Get direction
     *
     * @return boolean 
     */
    public function getDirection()
    {
        return $this->direction;
    }
}

When editing a category, I need to display the current values in a form, so that the user can modify them and save.

Here I have a controller editAction(), whose mission is to display the edition form:

   public function editAction($id) { //id of the category to modify

        $category = $this->getDoctrine()->getRepository('EvrHomeBundle:Category')->find($id); //return the category with id

        $categoryForm = $this->createForm(new CategoryType(),$category); //building the form


        return $this->render('EvrAdminBundle:Categories:edit.html.twig', array('categoryForm' => $categoryForm->createView(), 'category' => $category));
    }//render the form

Remember that the CategoryType has an element which type : entity, which loads the languages in A select box.

But when trying to populate the form CategoryType with the current data (Category and Language) , Symfony returns an Error : Entity Not Found

Symfony doesn't specify in which line the error occures, but I think it's around this line :

$categoryForm = $this->createForm(new CategoryType(),$category); //building the form

Because when I remove the second argument of createForm : $category, it displays an empty form (just like an add category form

Is there a solution for this issue? And how can I create a form with current data from the database, considering the fact that it contains an entity element.

Thank you

Try to use \ in front of entity namespace:

'class' => '\Evr\HomeBundle\Entity\Language',