我不能让Set :: sort在较低级别上运行

时间:2014-10-17 10:12:31

标签: php sorting cakephp

在我的应用中,我有以下模型和关系:

  • ActiveMember belongsTo Team
  • ActiveMember belongsTo Rank
  • ActiveMember hasMany ContactInfo
  • ContactInfo belongsTo ContactType

在我的控制器中,我是他们正在做一个包含的发现;

$this->Team->contain(array(
    'ActiveMember' => array (
        'fields' => array('id','surname','firstname'), 
        'Rank' => array(
            'fields' => array('id','display','position')) , 
                'ContactInfo' => array (
                    'ContactType',
                    'conditions' => 'contact_type_id IN (1, 2, 3, 4)'   
                )
            )
        )
    );
$Teams = $this->Team->find('all', array('conditions' => 'Team.active = 1 AND Team.display_order<99'));

然后返回一个这样的数组:

Array
(
    [0] => Array
        (
            [Team] => Array
                (
                    [id] => 3
                    [name] => Command
                    [display_order] => 1
                    [active] => 1
                )

            [ActiveMember] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [surname] => Bloggs
                            [firstname] => Joe
                            [rank_id] => 1
                            [team_id] => 3
                            [Rank] => Array
                                (
                                    [id] => 1
                                    [display] => Commander
                                    [position] => 1
                                )

                            [ContactInfo] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2
                                            [member_id] => 1
                                            [contact_type_id] => 1
                                            [info] => 1234 5678
                                            [ContactType] => Array
                                                (
                                                    [id] => 1
                                                    [name] => Home Phone
                                                    [format] => #### ####
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [surname] => Smith
                            [firstname] => Jane
                            [rank_id] => 2
                            [team_id] => 3
                            [Rank] => Array
                                (
                                    [id] => 2
                                    [display] => Dep Comm
                                    [position] => 10
                                )

                            [ContactInfo] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [Team] => Array
                (
                    [id] => 1
                    [name] => Support
                    [display_order] => 2
                    [active] => 1
                )

            [ActiveMember] => Array
                (
                    [0] => Array
                        (
                            [id] => 124
                            [surname] => Johnson
                            [firstname] => John
                            [rank_id] => 6
                            [team_id] => 1
                            [Rank] => Array
                                (
                                    [id] => 6
                                    [display] => Member 
                                    [position] => 50
                                )

                            [ContactInfo] => Array
                                (
                                )

                        )

然后我想根据'ORDER BY Team.display_order,Team.Member.Rank.position,Team.Member.surname,Team.Member.firstname'

对结果进行排序

鉴于我不能在没有进行adhoc连接的情况下在查询中进行这种复杂的排序,我想我可以使用Set :: sort,调用四次来对顺序进行排序。

$Teams = Set::sort($Teams,'{n}.ActiveMember.{n}.surname','asc');
$Teams = Set::sort($Teams,'{n}.ActiveMember.{n}.firstname','asc');
$Teams = Set::sort($Teams,'{n}.ActiveMember.{n}.Rank.position','asc');
$Teams = Set::sort($Teams,'{n}.Team.display_order','asc');

但是,尽管如果我执行Set :: extract,前三行的Set路径返回正确的值,它们对数组顺序没有任何影响(无论是作为一个组调用,还是作为唯一的排序) 。最后一行有效。

有人可以告诉我的错误在哪里,或者其他方法可以达到相同的结果吗?

0 个答案:

没有答案