2013-08-06 25 views
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; 
    } 
} 

然後,我有這個方法在我的控制器處理的形式提交:

/** 
* 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(), 
    )); 
} 

而且最後,這是我的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'); 
} 

當我試圖保存數據我得到這個錯誤:

An exception occurred while executing 'INSERT INTO category (name, parent, description, age_limit, created, modified) VALUES (?, ?, ?, ?, ?, ?)' with params ["Cat2", {}, null, 2, "2013-08-06 09:58:03", "2013-08-06 09:58:03"]:

Catchable Fatal Error: Object of class CategoryBundle\Entity\Category could not be converted to string in /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138

我在做什麼錯了?我如何訪問我的方法中的屬性,以便我可以保存該值?

UPDATE

基於由@sybio提出的建議,我所以現在我有這個重寫我的實體:

/** 
* @ManyToOne(targetEntity="Category") 
* @JoinColumn(name="parent", referencedColumnName="id") 
*/ 
protected $parent; 

是嗎?

+0

什麼是第60行? – cheesemacfly

+0

@cheesemacfly忘記了第60行,那個錯誤是由'echo'引起的,正確的錯誤在編輯後 – Reynier

回答

4

這裏我想:

您試圖保存一個類別對象作爲父母,但在下面的代碼:

/** 
* 
* @ORM\Column(type="integer", nullable=true) 
*/ 
protected $parent; 

你說,父母是一個整數,所以框架嘗試將父類別保存爲一個整數,但它的腳它可能轉換爲一個字符串的對象之前,所以它崩潰...

我不完全確定,但你應該重新考慮你的財產「$父」 。

它應該是self referencing relation

例子:

/** 
* @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(); 
} 

不要忘記重構你的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; 
    } 

希望這是解決方案!

+0

你完全正確,'$ parent'是你說的「自我關係」。由於這是我第一次使用Symfony2,我需要你的幫助,你可以看看我的版本嗎? – Reynier

+0

您的財產現在很好(您選擇一對多自引用單向關係),並且該字段將在數據庫中被命名爲「父」。檢查我的編輯我粘貼了你應該作爲getter和setter;)。請享用 ! – Sybio

+0

表示「多對一自引用單向」關係(不是「一對多」)* – Sybio

相關問題