Php 防止对具有find的hasMany模型进行关联查询

Php 防止对具有find的hasMany模型进行关联查询,php,cakephp,Php,Cakephp,我有一个与其他模型有很多关联的模型。我注意到的一点是,当我对父表进行查询时,所有相关联的表都会被查询。出于性能考虑,我希望避免这种情况,因为我不需要在每次调用父模型时都使用这些数据 这是我当前的父模型: class UserEntity extends UserAgentAppModel { var $name = 'UserEntity'; var $primaryKey = 'entity_id'; var $actsAs = array('EavEntity'); var $valida

我有一个与其他模型有很多关联的模型。我注意到的一点是,当我对父表进行查询时,所有相关联的表都会被查询。出于性能考虑,我希望避免这种情况,因为我不需要在每次调用父模型时都使用这些数据

这是我当前的父模型:

class UserEntity extends UserAgentAppModel {
var $name = 'UserEntity';
var $primaryKey = 'entity_id';
var $actsAs = array('EavEntity');

var $validate = array(
    'user_name'=>array(
        'rule'=>'isUnique',
        'message'=>'This username has already been taken. Please try again'
),
    'user_pass' => array(
        'rule' => array('between', 8, 16),
        'message' => 'Passwords must be between 8 and 16 characters long.')

);

var $hasMany = array(
    'UserEntityVarchar' => array(
        'className' => 'UserEntityVarchar',
        'foreignKey' => 'entity_id',
        'isEav' => 'true'
    ),
    'UserEntityDatetime' => array(
        'className' => 'UserEntityDatetime',
        'foreignKey' => 'entity_id',
        'isEav' => 'true'
    ),
    'UserEntityInteger' => array(
        'className' => 'UserEntityInteger',
        'foreignKey' => 'entity_id',
        'isEav' => 'true'
    ),
    'UserEntityBoolean' => array(
        'className' => 'UserEntityBoolean',
        'foreignKey' => 'entity_id',
        'isEav' => 'true'
    ),
    'UserEntityArray' => array(
        'className' => 'UserEntityArray',
        'foreignKey' => 'entity_id',
        'isEav' => 'true'
    )
);

 );?>
这是我在查询日志中看到的。我看到的问题是,在使用find时,查询12-17总是出现。然而,我正在使用一种行为从我的eav模型中提取这些数据

1   SHOW FULL COLUMNS FROM `user_entities`      8   8   1
2   SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= 'latin1_swedish_ci';     1   1   1
3   SHOW FULL COLUMNS FROM `user_entity_varchars`       4   4   1
4   SHOW FULL COLUMNS FROM `user_entity_datetimes`      4   4   1
5   SHOW FULL COLUMNS FROM `user_entity_integers`       4   4   1
6   SHOW FULL COLUMNS FROM `user_entity_booleans`       4   4   1
7   SHOW FULL COLUMNS FROM `user_entity_arrays`     4   4   1
12  SELECT `UserEntity`.`entity_id`, `UserEntity`.`user_name`, `UserEntity`.`user_pass`, `UserEntity`.`user_status`, `UserEntity`.`user_group`, `UserEntity`.`instance_id`, `UserEntity`.`is_logged_in`, `UserEntity`.`is_visible` FROM `user_entities` AS `UserEntity` WHERE 1 = 1     2   2   0
13  SELECT `UserEntityVarchar`.`value_id`, `UserEntityVarchar`.`attribute_id`, `UserEntityVarchar`.`entity_id`, `UserEntityVarchar`.`value` FROM `user_entity_varchars` AS `UserEntityVarchar` WHERE `UserEntityVarchar`.`entity_id` IN (1, 2)      3   3   0
14  SELECT `UserEntityDatetime`.`value_id`, `UserEntityDatetime`.`attribute_id`, `UserEntityDatetime`.`entity_id`, `UserEntityDatetime`.`value` FROM `user_entity_datetimes` AS `UserEntityDatetime` WHERE `UserEntityDatetime`.`entity_id` IN (1, 2)       0   0   0
15  SELECT `UserEntityInteger`.`value_id`, `UserEntityInteger`.`attribute_id`, `UserEntityInteger`.`entity_id`, `UserEntityInteger`.`value` FROM `user_entity_integers` AS `UserEntityInteger` WHERE `UserEntityInteger`.`entity_id` IN (1, 2)      0   0   0
16  SELECT `UserEntityBoolean`.`value_id`, `UserEntityBoolean`.`attribute_id`, `UserEntityBoolean`.`entity_id`, `UserEntityBoolean`.`value` FROM `user_entity_booleans` AS `UserEntityBoolean` WHERE `UserEntityBoolean`.`entity_id` IN (1, 2)      0   0   0
17  SELECT `UserEntityArray`.`value_id`, `UserEntityArray`.`attribute_id`, `UserEntityArray`.`entity_id`, `UserEntityArray`.`value` FROM `user_entity_arrays` AS `UserEntityArray` WHERE `UserEntityArray`.`entity_id` IN (1, 2)        0   0   0
22  SHOW FULL COLUMNS FROM `eav_attributes`     8   8   1
23  SELECT `EavAttribute`.`attribute_id`, `EavAttribute`.`attribute_code`, `EavAttribute`.`backend_model`, `EavAttribute`.`frontend_input`, `EavAttribute`.`frontend_label`, `EavAttribute`.`is_required`, `EavAttribute`.`is_user_defined`, `EavAttribute`.`is_unique` FROM `eav_attributes` AS  `EavAttribute` WHERE `attribute_id` = 5       1   1   0
24  SELECT `EavAttribute`.`attribute_id`, `EavAttribute`.`attribute_code`, `EavAttribute`.`backend_model`, `EavAttribute`.`frontend_input`, `EavAttribute`.`frontend_label`, `EavAttribute`.`is_required`, `EavAttribute`.`is_user_defined`, `EavAttribute`.`is_unique` FROM `eav_attributes` AS `EavAttribute` WHERE `attribute_id` = 6        1   1   0
25  SELECT `EavAttribute`.`attribute_id`, `EavAttribute`.`attribute_code`, `EavAttribute`.`backend_model`, `EavAttribute`.`frontend_input`, `EavAttribute`.`frontend_label`, `EavAttribute`.`is_required`, `EavAttribute`.`is_user_defined`, `EavAttribute`.`is_unique` FROM `eav_attributes` AS `EavAttribute` WHERE `attribute_id` = 7
正确使用cake的功能,将您的查询限制在有用的数据范围内

检查两者的工作原理,你会得到一个更好的想法。

正确使用cake的功能,将你的查询限制到有用的数据


检查两者的工作原理,你会有更好的想法。

如果你不想在find查询中提取所有hasmall数据,请将recursive的值设置为-1,就像在控制器中一样

   $results = $this->Model->find('all', 'recursive' => -1));

一个更好的选择是使用可包含的行为,通过这种方式,您可以指定要获取哪些模型,哪些不获取

如果不想提取find查询中的所有hasMany数据,请将recursive的值设置为-1,就像在控制器中一样

   $results = $this->Model->find('all', 'recursive' => -1));

一个更好的选择是使用可包含的行为,通过这种方式,您可以指定要获取哪些模型,哪些不获取

检查这个。。。检查这个…我通常在任何有关联的模型上使用可包含行为。除非您不关心性能,否则这是IMO的基本要求。我通常在具有关联的任何模型上使用Containable行为。除非您不关心性能,否则这是IMO的基本要求。