我正在使用 CakePHP 2.5.2 并且在有效地搜索数据时遇到了一些麻烦。
在我的应用程序中,我有 3 个表格,团队,球员,技能......在团队中有 80 条记录,玩家 2400 条记录,技能 2400 条记录......我想计算一个团队的平均技能......
//Team model
public $actsAs = array('Containable');
public $hasMany = array('Player');
//Player model
public $actsAs = array('Containable');
public $hasOne = array('Skill');
public $belongsTo = array('Team');
//Skill model
public $actsAs = array('Containable');
public $belongsTo = array('Player');
我的研究是:
$team = $this->Team->find('all', array(
'contain' => array(
'Player' => array(
'Skill'
)
),
));
$this->set('team', $team);
这给出了预期的结果:
Array
(
[0] => Array
(
[Team] => Array
(
[id] => 1
[name] => my_team_name
)
[Player] => Array
(
[0] => Array
(
[id] => 000000419
[name] => Name
[surname] => Surname
[age] => 21
[team_id] => 1
[Team_id] => 1
[Skill] => Array
(
[id] => 20
[player_id] => 000000419
[skill] => 599
)
), ecc.....
这个结构至少使用了 1680 个查询……这对我来说太多了……
我尝试了另一种方式,只涉及一个查询,返回一个错误的数据结构,但我需要的所有信息(也是多余的)。不幸的是,按照这种方式,我无法在 View 中迭代以显示我需要的内容。
$player = $this->Team->Player->find('all', array(
'contains' => array(
'Skill',
),
返回
Array
(
[0] => Array
(
[Player] => Array
(
[id] => 000000400
[nome] => my_player_name
[cognome] => my_player_surname
[nation_id] => 380
[age] => 29
[team_id] => 2
)
[Team] => Array
(
[id] => 2
[nome] => my_team_name
)
[Skill] => Array
(
[id] => 1
[player_id] => 000000400
[average] => 632
)
)
ecc.
有没有办法在 VIEV 中进行迭代以获得每个团队的平均技能?还有其他解决方案吗?
谢谢!