我开始使用 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?
发布评论
评论(3)
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
首先尝试使用正确的
递归
级别,然后使用@riky所述的unBindModel
。不要做一些愚蠢的事情,比如使用递归级别 2,然后取消绑定所有不需要的模型。
Try using proper
recursive
level first, After that useunBindModel
as stated by @riky.Don't do something silly like using
recursive
level 2 and after that unbinding all the unwanted models.每次都会加载那些相关数据吗?
数据:没有。模型:是的,相关模型将被初始化。因为根据当前视图,某些链接表永远不会被使用,实际上每次都会查询它们。
使用可包含或递归
。这给我们带来了很大的成本,不是吗?
嗯,也许吧,如果你有很多关系的话。否则,只是更方便而已。那么如何拥有这种关系并仅在需要时才激活它呢?某种延迟加载仅在我需要时才加载相关表?
我认为延迟加载不可用。您始终可以动态绑定模型。但我再说一次,你担心的是微小的优化。每个请求加载这些模型通常需要几毫秒。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 orrecursive
.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.