symfony2表单中找不到实体错误 [英] Entity Not Found error in symfony2 Form

查看:252
本文介绍了symfony2表单中找不到实体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

正如你所料,我有两个实体类别和语言,其中是语言的孩子(一种语言可以有很多类别,一类属于1或0种语言)



类别

 <?php 

命名空间Evr\HomeBundle\Entity;

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\Mapping作为ORM;

/ **
*类别
*
* @ ORM\Table(name =ev_category)
* @ ORM\Entity
* /
class类别
{
/ **
* @var整数
*
* @ ORM\Column(name = category =,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 $子类别;

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

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

/ **
*设置语言
*
* @param integer $ language
* @return类别
* /
public function setLanguage($ language){
$ this-> language = $ language;

return $ this;
}

/ **
*获取语言
*
* @return integer
* /
public function getLanguage( ){
return $ this-> language;
}

/ **
*设置类别
*
* @param \string $ category
* @return类别
* /
public function setCategory($ category)
{
$ this-> category = $ category;

return $ this;
}

/ **
*获取类别
*
* @return \string
* /
public function getCategory()
{
return $ this-> category;
}

}

语言

 <?php 

命名空间Evr\HomeBundle\Entity;

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\Mapping作为ORM;

/ **
*语言
*
* @ ORM\Table(name =ev_language)
* @ ORM\Entity
* /
class语言
{
/ **
* @var整数
*
* @ 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();
}

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

/ **
*设置语言
*
* @param string $ language
* @return语言
* /
public function setLanguage($ language)
{
$ this-> language = $ language;

return $ this;
}

/ **
*获取语言
*
* @return string
* /
public function getLanguage )
{
return $ this-> language;
}

/ **
*设置代码
*
* @param string $ code
* @return语言
* /
public function setCode($ code)
{
$ this-> code = $ code;

return $ this;
}

/ **
*获取代码
*
* @return string
* /
public function getCode )
{
return $ this-> code;
}

/ **
*设置方向
*
* @param boolean $ direction
* @return语言
* /
public function setDirection($ direction)
{
$ this-> direction = $ direction;

return $ this;
}

/ **
*获取方向
*
* @return boolean
* /
public function getDirection )
{
return $ this-> direction;
}
}

编辑类别时,我需要显示当前值为一个表单,以便用户可以修改它们并保存。



这里我有一个控制器editAction(),其任务是显示版本格式: p>

  public function editAction($ id){//要修改的类别的ID 

$ category = $这 - > getDoctrine() - > getRepository( 'EvrHomeBundle:类别') - >找到($ ID); //返回类别为

$ categoryForm = $ this-> createForm(new CategoryType(),$ category); //构建窗体


返回$ this-> render('EvrAdminBundle:Categories:edit.html.twig',array('categoryForm'=> $ categoryForm-> createView(),'category'=> $ category));
} //呈现表单

请记住,CategoryType有一个类型为entity的元素,它在A选择框中加载语言。



但是,当尝试使用当前数据(类别和语言)填充表单CategoryType时,Symfony返回错误:实体未找到



Symfony没有指定错误发生在哪一行,但我认为它在这一行: / p>

  $ categoryForm = $ this-> createForm(new CategoryType(),$ category); //建立窗体

因为当我删除 createForm $ $ category ,它显示一个空的表单(就像一个添加类别表单



有没有解决这个问题的方法?如何从数据库创建一个包含当前数据的表单,考虑到它包含一个实体元素。



谢谢

解决方案

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

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

这篇关于symfony2表单中找不到实体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆