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

时间:2011-09-16 05:41:08

标签: php database cakephp frameworks lazy-loading

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

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

那么如何建立这种关系并在我们需要时激活它呢?某种延迟加载只在我需要时加载相关表格?

3 个答案:

答案 0 :(得分:1)

Cake facilates解除你的非必需模型的攻击你运行查询。

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

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

$ model - 型号名称。

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

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

答案 1 :(得分:1)

首先尝试使用正确的recursive级别,之后使用@riky所述的unBindModel。 不要像使用recursive级别2那样愚蠢,然后解除所有不需要的模型的绑定。

答案 2 :(得分:1)

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.使用可包含的或recursive

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?我不认为延迟加载是可用的。您可以随时使用bindModel。但同样,我会说你担心微小的优化。加载这些模型通常会在每个请求中花费几个小时。