学说:设置与 id 而不是对象实例的关联 [英] doctrine: setting association with id instead of object instance

查看:20
本文介绍了学说:设置与 id 而不是对象实例的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像这样设置两个对象之间的关联,例如articlecomment:

is it possible to set associations between two objects, for example article and comment like this:

comment.setArticle(10) // 10 is the id of article

自动生成的 setArtcicle 方法当然将文章作为参数对象,但也许有一些技巧可以做到这一点?

autogenerated setArtcicle methods takes as argument object Article of course but maybe there are some tricks to do this?

从性能的角度来看,这对我来说非常重要 - 当我想设置关联时,我想避免总是进行 SQL 调用.就我而言,将有大量此类不必要的查询.

It is very important to me from performance point of view - i would like to avoid making SQL calls always when I want to set an association. In my case there will be plenty of such unnecessary queries.

推荐答案

您可以使用:

$comment->setArticle($em->getReference('Article', 10));

这篇关于学说:设置与 id 而不是对象实例的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆