2014-01-27 60 views
0

我一直在掙扎學說,我已經得到了博客文章的實體,它引用的意見和它拋出以下錯誤: 注意:未定義指數:POST_ID學說2協會投擲警告

這也得到所有的評論,無論post_id。這裏是我的映射:

/** 
    * @OneToMany(targetEntity="CommentsBundle\Entities\Comments", mappedBy="post_id") 
    */ 
    protected $comments; 

編輯

這裏的評論實體:

<?php 

namespace CommentsBundle\Entities; 

use Doctrine\ORM\Mapping AS ORM; 

/** 
* @Entity @Table(name="comments") 
**/ 
class Comments 
{ 
    /** @Id @Column(type="integer") @GeneratedValue */ 
    private $id; 

/** @Column(type="string") */ 
protected $name; 

/** @Column(type="string") */ 
protected $email; 

/** @Column(type="string") */ 
protected $content; 

/** @Column(type="string") */ 
protected $date; 

/** @Column(type="integer") */ 
protected $user_id; 

/** @Column(type="integer") */ 
protected $post_id; 

/** 
* @ORM\ManyToOne(targetEntity="ContentBundle\Entities\Posts", inversedBy="comments") 
* @ORM\JoinColumn(name="post_id", referencedColumnName="id") 
*/ 
protected $post; 

public function setId($id) 
{ 
    $this->id = $id; 

    return $this; 
} 

public function getId() 
{ 
    return $this->id; 
} 

public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

public function getName() 
{ 
    return $this->name; 
} 

public function setEmail($email) 
{ 
    $this->email = $email; 

    return $this; 
} 

public function getEmail() 
{ 
    return $this->email; 
} 

public function setContent($content) 
{ 
    $this->content = $content; 

    return $this; 
} 

public function getContent() 
{ 
    return $this->content; 
} 

public function setDate($date) 
{ 
    $this->date = $date; 

    return $this; 
} 

public function getDate() 
{ 
    return $this->date; 
} 

public function setUser_id($user_id) 
{ 
    $this->user_id = $user_id; 
} 

public function getUser_id() 
{ 
    return $this->user_id; 
} 

public function setPost_id($post_id) 
{ 
    $this->post_id = $post_id; 

    return $this; 
} 

public function getPost_id() 
{ 
    return $this->post_id; 
} 
} 

提前感謝!

+0

是不是在Symfony2(你似乎使用)中的實體目錄稱爲「實體」而不是「實體」? – Jivan

+0

我實際上使用Silex,我的大部分代碼都是這樣保存的 src/BundleName/Controllers src/BundleName/Entities src/BundleName/Views –

+0

ok。你關係的另一面是什麼?你可以編輯你的帖子來顯示這個? – Jivan

回答

1

你必須在實體的屬性上寫你的mappedBy,而不是列。

基本上,實體的財產必須「互相交談」。

$comments$post映射,$post$comments反轉:

class Posts 
{ 
    /** 
    * @OneToMany(targetEntity="CommentsBundle\Entities\Comments", mappedBy="post") 
    */ 
    protected $comments; 
} 

class Comments 
{ 
    /** 
    * @ORM\ManyToOne(targetEntity="ContentBundle\Entities\Posts", inversedBy="comments") 
    * @ORM\JoinColumn(name="post_id", referencedColumnName="id") 
    */ 
    protected $post; 
} 

而且,我就不會在你的Comments實體定義$post_id。只是$post,當您需要檢索的帖子的ID,那麼:

public function getPost_id() 
{ 
    return $this->post->getId(); 
} 

事情是清潔這種方式。

+0

完美,非常感謝! :d –