2014-01-27 43 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 –