这些天我很忙于学说。但我对关联感到困惑。虽然我指定一个实体,但不幸的是我不能在两个表之间进行关联。 one2many,many2one或其他。与学说2.1的关联
我在website阅读关于关联映射的文档。我很困惑。
我想做一个简单的例子。请帮帮我 !
我们走吧!
我有两个表。其中之一是用户和另一个是评论。我知道这很简单,但一切都是为了我的恩惠。
用户表有三列是ID,名,COMMENT_ID。
评论表有ID和COMMENT_TEXT列。 (我认为,问题是在这里)
,现在我users.php文件
/** @Entity @Table(name="users") */
class User{
/** @Id @GeneratedValue @Column(type="integer") */
private $id;
/** @Column(name="name", length=55, type="string") */
private $name;
/**
* @OneToMany(targetEntity="Comment", mappedBy="author")
*/
private $commentID;
public function addComment($comment){
$this->commentID[] = $comment;
}
public function setName($new){
$this->name = $new;
}
public function getName(){
return $this->name;
}
public function __construct()
{
$this->commentID = new Doctrine\Common\Collections\ArrayCollection();
}
}
和的comments.php文件
/** @Entity @Table(name="comments") */
class Comment{
/** @Id @GeneratedValue @Column(name="author", type="integer")
* @ManyToOne(targetEntity="User", inversedBy="commentID")
*/
private $author;
/** @Column(name="comment_text", type="text") */
private $commentText;
public function setAuthor($author)
{
$author->addComment($this);
$this->author = $author;
}
public function setComment($new){
$this->commentText = $new;
}
public function getComment(){
return $this->commentText;
}
}
就是这样。我想插入,选择,更新和删除两个表中的项目。
$person = new User();
$person->setName('Jack Sparrow');
$comment = new Comment();
$comment->setComment('Hello pirates !');
$comment->setAuthor($person);
$em->persist($person);
$em->persist($comment);
$em->flush();
该代码工作良好,但的值COMMENT_ID柱总是为零= 0 用户表。我想我在某个地方有一个很大的错误。
帮助我错了!
问候....
@JOHN; 10倍很多。这是非常有帮助的。 – kakashi