未发现Symfony2形式的实体错误



我有一个symfony2表单类别tatepe with buildform:

  public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('language', 'entity', array(
                    'class' => 'EvrHomeBundleEntityLanguage',
                    'property' => 'language'
                        )
                )
                ->add('category', 'text', array('label' => 'category.category', 'required' => true));
    }

您可以预期,我有两个实体类别和语言,其中哪个类别是语言的孩子(一种语言可以有许多类别,一个类别属于1或0语言)

类别

<?php
namespace EvrHomeBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Category
 *
 * @ORMTable(name="ev_category")
 * @ORMEntity
 */
class Category
{
    /**
     * @var integer
     *
     * @ORMColumn(name="category_id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
     /**
     * 
     * @ORMManyToOne(targetEntity="Language",inversedBy="categories")
     * @ORMJoinColumn(name="language_id",referencedColumnName="language_id")
     */
    private $language;
    /**
     * @var string
     *
     * @ORMColumn(name="category", type="string", length=255)
     */
    private $category;
    /**
     * @ORMOneToMany(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;
    }
}

语言

<?php
namespace EvrHomeBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Language
 *
 * @ORMTable(name="ev_language")
 * @ORMEntity
 */
class Language
{
    /**
     * @var integer
     *
     * @ORMColumn(name="language_id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="language", type="string", length=128)
     */
    private $language;
    /**
     * @var string
     *
     * @ORMColumn(name="code", type="string", length=10)
     */
    private $code;
    /**
     * @var boolean
     *
     * @ORMColumn(name="direction", type="boolean")
     */
    private $direction;
    /**
     * @ORMOneToMany(targetEntity="Category", mappedBy="language")
     */
    protected $categories;
     /**
     * @ORMOneToMany(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;
    }
}

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

在这里,我有一个控制器Editaction(),其任务是显示版本:

   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

请记住,类别类型的元素类型:实体,将语言加载在选择框中。

但是,当试图用当前数据(类别和语言)填充表单类别类型时,Symfony返回错误:Entity Not Found

Symfony并未指定错误的发生行,但我认为这是围绕这一行的:

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

因为当我删除createForm$category的第二个参数时,它会显示一个空形式(就像add category form

一样

有解决这个问题的解决方案吗?考虑到它包含实体元素的事实,我如何使用数据库中的当前数据创建表单。

谢谢

如果您的数据库没有引用完整性,则可以删除记录,即使另一个记录指向它(它们应该通过外键链接)。然后,当您尝试获取记录时,它似乎有效,但是当您要访问该实体的属性(这是对数据库的真正访问发生的时候)时,它找不到使用Record_ID的实体,因此找不到"未找到实体"。

ex:

$user=new User();
$car=$user->getCar();    /*The user had a CAR record assigned with Id = 1 but the record was 
                           deleted.              
                           The record USER still has the id of 1.*/
/*No error up till here*/
$carName=$car->getName();    /*Error occurs because it tries to grab the record with id of 1 
                               which was deleted*/

尝试在实体名称空间前使用

'class' => 'EvrHomeBundleEntityLanguage',

相关内容

  • 没有找到相关文章

最新更新