2012-06-26 66 views
0

我有5个有关系的tabels。Symfony2,Doctrine数据持久性

表用户和文章有关系:

用户:

/** 
* @ORM\OneToMany(targetEntity="Article", mappedBy="author") 
*  
* @var ArrayCollection $articles 
*/ 
protected $articles; 

在文章:

/** 
* @ORM\ManyToOne(targetEntity="User", inversedBy="articles") 
* 
*/ 
protected $author; 

我创造新的文章:

$article = new Article(); 
$article->setTitle("title"); 
$article->setText("Text test"); 
$article->setType("image"); 
$em->persist($article); 
$em->flush(); 

我添加第t条o用户:

$user->addarticle($article); 
$em->persist($user); 
$em->flush(); 

文章保存在数据库中,但它没有任何字段的数据:author_id

当我向用户添加一篇文章到automaticaly保存在表文章中时,如何建立关系为author_id

回答

0

试试这个模式。 用户:

/** 
* @ORM\OneToMany(targetEntity="Article", mappedBy="author") 
*/ 
protected $articles; 

在文章:

/** 
* @ORM\ManyToOne(targetEntity="User", inversedBy="articles") 
* @ORM\JoinColumn(name="author_id", referencedColumnName="id") 
*/ 
protected $author; 

Genereate实体,以及更新的架构。

$article = new Article(); 
$article->setTitle("title"); 
$article->setText("Text test"); 
$article->setType("image"); 
$article->setAuthor($user); 
$em->persist($article); 
$em->flush();