Rey*_*ier 3 symfony-forms symfony symfony-2.3
我定义了这个实体:
namespace CategoryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
*
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $name;
/**
*
* @ORM\Column(type="integer", nullable=true)
*/
protected $parent;
/**
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $description;
/**
*
* @ORM\Column(type="integer")
*/
protected $age_limit;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created", type="datetime")
*/
protected $created;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="modified", type="datetime")
*/
protected $modified;
public function getId() {
return $this->id;
}
public function setParent(Category $parent = null) {
$this->parent = $parent;
}
public function getParent() {
return $this->parent;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function setDescription($description) {
$this->description = $description;
}
public function getDescription() {
return $this->description;
}
public function setAgeLimit($age_limit) {
$this->age_limit = $age_limit;
}
public function getAgeLimit() {
return $this->age_limit;
}
public function setCreated($created) {
$this->created = $created;
}
public function getCreated() {
return $this->created;
}
public function setModified($modified) {
$this->modified = $modified;
}
public function getModified() {
return $this->modified;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的控制器中有这个方法来处理表单提交:
/**
* Handle category creation
*
* @Route("/category/create", name="category_create")
* @Method("POST")
* @Template("CategoryBundle:Default:new.html.twig")
*/
public function createAction(Request $request) {
$entity = new Category();
$form = $this->createForm(new CategoryType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('category_list'));
}
return $this->render('CategoryBundle:Default:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
Run Code Online (Sandbox Code Playgroud)
最后这是我的CategoryType.php:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('parent', 'entity', array('class' => 'CategoryBundle:Category', 'property' => 'name', 'required' => false))
->add('name')
->add('description')
->add('age_limit');
}
Run Code Online (Sandbox Code Playgroud)
当我尝试保存数据时,出现此错误:
使用参数 ["Cat2", {}, null, 2 执行 'INSERT INTO category (name, parent, description, age_limit, created, modified) VALUES (?, ?, ?, ?, ?, ?)' 时发生异常, "2013-08-06 09:58:03", "2013-08-06 09:58:03"]:
可捕获的致命错误:无法在 /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php 行 138 中将类 CategoryBundle\Entity\Category 的对象转换为字符串
我做错了什么?如何在我的方法中访问该属性以便我可以保存该值?
更新
根据@sybio 提出的建议,我重写了我的实体,所以现在我有了:
/**
* @ManyToOne(targetEntity="Category")
* @JoinColumn(name="parent", referencedColumnName="id")
*/
protected $parent;
Run Code Online (Sandbox Code Playgroud)
那正确吗?
这是我的想法:
您正在尝试将 Category 对象保存为父对象,但在以下代码中:
/**
*
* @ORM\Column(type="integer", nullable=true)
*/
protected $parent;
Run Code Online (Sandbox Code Playgroud)
你说父类是一个整数,所以框架尝试将父类保存为一个整数,但为了让它之前可能将对象转换为字符串,所以它崩溃了......
我不完全确定,但您应该重新考虑您的财产“$parent”。
它应该是一个自引用关系。
例子 :
/**
* @OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* @ManyToOne(targetEntity="Category", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
public function __construct() {
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
不要忘记重构你的 getter / setter :
/**
* Set parent
*
* @param \CategoryBundle\Entity\Category $parent
*/
public function setParent(\CategoryBundle\Entity\Category $parent = null)
{
$this->parent = $parent;
}
/**
* Get parent
*
* @return \CategoryBundle\Entity\Category
*/
public function getParent()
{
return $this->parent;
}
Run Code Online (Sandbox Code Playgroud)
希望这是解决方案!
| 归档时间: |
|
| 查看次数: |
14574 次 |
| 最近记录: |