php - Doctrine 2.1 - 实体插入

标签 php doctrine doctrine-orm

我对将实体插入数据库有疑问。我有两个模型:

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;
    }

}

要添加新新闻,我必须包括 UserNews 类(如果它们在单独的文件中,例如 UserModel.php 和 NewsModel.php)和写一段代码:

$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($database->find('User', 1));
$database->persist($news);

我的问题是:有没有办法不包含 User 类就可以插入新闻?

最佳答案

您不需要实际加载用户。

相反,您可以使用 reference proxy :

<?PHP
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($em->getReference('User',1));
$em->persist($news);

关于php - Doctrine 2.1 - 实体插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8703102/

相关文章:

doctrine-orm - Doctrine 2 通过复合键一对一

php preg_split 从方括号中拆分字符串到数组

php - 交响乐 2 : how to reduce this code?

php - 交响乐 : Doctrine data fixture : how to handle large csv file?

symfony - 级联= {"remove"} VS orphanRemoval=true VS ondelete="CASCADE

PHPUnit 测试和 Doctrine,连接太多

mongodb - 绑定(bind)学说ORM实体和学说ODM文档,在SonataAdminBundle中使用

php - 如何使用php提取youtube视频数据?

PHP include vs include_once(速度)

javascript - AJAX 调用时出现错误 (net::ERR_EMPTY_RESPONSE)