我有关于将实体插入数据库的问题。我有两个型号:学说2.1 - 实体插入
class News {
/**
* @Column(type="string", length=100)
* @var string
*/
protected $title;
/**
* @ManyToOne(targetEntity="User", inversedBy="news")
* @JoinColumn(referencedColumnName="id")
*/
protected $author;
}
class User {
/**
* @Id @GeneratedValue @Column(type="integer")
* @var integer
*/
protected $id;
/**
* @OneToMany(targetEntity="News", mappedBy="author")
*/
protected $news;
public function __construct() {
$this->news = new \Doctrine\Common\Collections\ArrayCollection;
}
}
要添加新的消息,我必须包括User
和News
类(如果他们是在不同的文件中,对前UserModel.php和NewsModel.php)和写代码:
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($database->find('User', 1));
$database->persist($news);
我的问题是:是否有任何方式插入新闻,而不包括User
类?
您的意思是:如何在没有用户的情况下制作新闻,因为新闻不应该有用户? – hakre 2012-01-02 16:32:19
我的意思是说,我只能给用户的ID而不是整个用户对象。例如。像'$ news-> setAuthor(1);'或'INSERT INTO新闻值('测试标题',1);' – 2012-01-02 16:34:52