cakePhp 中的外键定义:延迟加载?

发布于 2024-12-04 15:03:57 字数 179 浏览 5 评论 0 原文

我开始使用 cakePhp 制作一个小网站。我定义了表之间的关系,我想知道:每次都会加载那些相关数据吗?因为根据当前视图的不同,有些链接表永远不会被使用,而实际上它们每次都会被查询。

它给我们带来的代价是很大的,不是吗?

那么如何才能拥有这种关系并只在需要的时候才激活它呢?某种延迟加载仅在我需要时才加载相关表?

I'm starting to use cakePhp to make a small website. I defined relationship between table, and I want to know: will those related data loaded every time? Because depending of the current view, some linked table will never be used, and actually they are queried every time.

It's a big cost for what it brings to us, no?

So how to have this kind of relationship and activate it only when we need it? Some kind of lazy loading which loads the related table only if I need it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下的泡沫是彩色的 2024-12-11 15:03:57

Cake 有助于在运行查询之前解除非必需模型的绑定。

$this->unbindModel(array($relation => $model));

$relation - 是您与其他模型的关系。

$model - 型号名称。

例如:$this->Library->unbindModel(array('belongsTo' => array('Membership'),),false);

http://bakery.cakephp.org/articles/cornernote/2006/12/10/unbindall

Cake facilates to unbind your non require model befire you run your query.

$this->unbindModel(array($relation => $model));

$relation - Is your relation with your other model.

$model - Model Name.

eg: $this->Library->unbindModel(array('belongsTo' => array('Membership'),),false);

http://bakery.cakephp.org/articles/cornernote/2006/12/10/unbindall

撩起发的微风 2024-12-11 15:03:57

首先尝试使用正确的递归级别,然后使用@riky所述的unBindModel
不要做一些愚蠢的事情,比如使用递归级别 2,然后取消绑定所有不需要的模型。

Try using proper recursive level first, After that use unBindModel as stated by @riky.
Don't do something silly like using recursive level 2 and after that unbinding all the unwanted models.

满意归宿 2024-12-11 15:03:57

每次都会加载那些相关数据吗? 数据:没有。模型:是的,相关模型将被初始化。

因为根据当前视图,某些链接表永远不会被使用,实际上每次都会查询它们。使用可包含或递归

这给我们带来了很大的成本,不是吗?嗯,也许吧,如果你有很多关系的话。否则,只是更方便而已。

那么如何拥有这种关系并仅在需要时才激活它呢?某种延迟加载仅在我需要时才加载相关表? 我认为延迟加载不可用。您始终可以动态绑定模型。但我再说一次,你担心的是微小的优化。每个请求加载这些模型通常需要几毫秒。

will those related data loaded every time? The data: no. The models: yes, related models will be initialized.

Because depending of the current view, some linked table will never be used, and actually they are queried every time. Use containable or recursive.

It's a big cost for what it brings to us, no? Well, maybe, if you have a lot of relationships. Otherwise, it's just more convenient.

So how to have this kind of relationship and activate it only when we need it? Some kind of lazy loading which loads the related table only if I need it? I don't think lazy loading is available. You can always bindModel on the fly. But again, I'd say you are worrying about tiny optimizations. Loading those models would usually take a few milisec in each request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文