我有一个带有buildForm的symfony2 Form CategoryType:
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
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;
}
}
语言
<?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;
}
}
编辑类别时,我需要在表单中显示当前值,以便用户可以修改它们并保存。
这里我有一个控制器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
请记住,CategoryType有一个类型为:entity的元素,它在A选择框中加载语言。
但是当尝试使用当前数据(类别和语言)填充表单CategoryType时,Symfony会返回错误:Entity Not Found
Symfony没有指定错误发生在哪一行,但我认为它就在这一行:
$categoryForm = $this->createForm(new CategoryType(),$category); //building the form
因为当我删除createForm
:$category
的第二个参数时,它会显示一个空表单(就像add category form
这个问题有解决方案吗?如何使用数据库中的当前数据创建表单,考虑到它包含实体元素。
谢谢
答案 0 :(得分:4)
如果您的数据库没有强制执行参照完整性,那么您可以删除记录,即使另一条记录指向它(它们应该通过外键链接)。然后,当你试图获取记录它似乎工作,但当你想访问该实体的属性(这是对数据库的实际访问时),它找不到具有该record_id的实体,因此“实体未找到”
例如:
$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*/
答案 1 :(得分:1)
尝试在实体名称空间前使用\
:
'class' => '\Evr\HomeBundle\Entity\Language',