1

我正在使用 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 中进行迭代以获得每个团队的平均技能?还有其他解决方案吗?

谢谢!

4

2 回答 2

1

如果你可以将 CakePHP 升级到 2.6 或更高版本,你可以使用我的插件来解决这个问题。该插件与 ContainableBehavior 具有很高的兼容性,但会生成更好的查询。我认为 find 操作将只执行 2 个查询。如果你尝试一下,我会很高兴的。

https://github.com/chinpei215/cakephp-eager-loader

用法

1.启用EagerLoader插件

// In your model
$actsAs = ['EagerLoader.EagerLoader'];

如果您担心加载我的插件会在某处破坏某些内容,您也可以即时启用它。

// On the fly
$this->Team->Behaviors->load('EagerLoader.EagerLoader');

2.执行同样的find操作

$this->Team->find('all', ['contain' => ['Player' => ['Skill']]]);

3.查看查询日志

您将看到如下查询日志:

SELECT ... FROM teams AS Team WHERE 1 = 1;
SELECT ... FROM players AS Player LEFT JOIN skills AS Skill ON Player.id = Skill.player_id WHERE Player.id IN ( ... );
于 2016-02-13T13:40:56.687 回答
-2
  1. 如果您觉得查询搜索了这么多表(即模型),那么您可以在使用 find() 执行搜索之前取消绑定这些模型

  2. 如果要获取表的某些特定列,则通过在 find() 中选择“字段”来删除其他列。

于 2014-07-04T12:32:49.047 回答