加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

cakephp查询与许多表

发布时间:2020-12-13 22:45:10 所属栏目:PHP教程 来源:网络整理
导读:我有一个在cakephp 2.0中开发的网站,我在这里有一些关联的表是一个例子: 这是我的关系: ingredients (id,name) has many versions ingredient_properties(id,property_id,version_id) belongs to properties,versions properties (id,name,value,group_id,
我有一个在cakephp 2.0中开发的网站,我在这里有一些关联的表是一个例子:
这是我的关系:

ingredients (id,name) has many versions

ingredient_properties(id,property_id,version_id) belongs
to properties,versions

properties (id,name,value,group_id,unit_id) has many
ingredient_properties and belongs to groups,units

groups (id,name) has many properties

units (id,name) has many properties

versions (id,ingredient_id,active) has many ingredient_properties and belongs to ingredients.

我在ingredientController.php中,我想要检索Version.active = 1和Version.ingredient_id = 2的所有数据.

这是我的查询:

$this->set(
        'ingredient',$this->Ingredient->Version->find('all',array(
            'recursive' => 2,'conditions' => array(
                'Version.active' => 1,'Version.ingredient_id' => 2
             )
        ))
);

我有很多这样的查询,我想知道递归2是否是检索我所解释的表的所有数据的最佳方法,或者有更快的方法(在查询速度方面没有实现) .
我希望有人可以帮助我优化我的代码,因为这个查询有效,但我不知道这是否是检索许多表关系数据的更好方法.

谢谢.

解决方法

这不是使用’recursive’=>的最佳方式. 2如果要检索如此多的数据.我相信它会产生太多查询.可容忍的行为具有相同的缺点.对我来说最好的方法是在 unbind models associations和 construct table joins飞行.您可以查看示例 here.但是您需要了解一些SQL以了解您的操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读