2012-02-28 75 views
1

只是一个大约的OrderBy快速的问题CakePHP中找到操作。假设我有3个模型相互关联。当我做任何我的3种型号的find('all') CakePHP的查询,我得到的结果还包括来自其他2款机型的数据。例如,假设我的车型有:CakePHP的顺序由另一个模型字段关联

1- User 
2- School 
3- Country 

如果我做$this->find('all')UsersController,因为我的三个型号都连在一起,我会得到这样的:

Array 
(
    [0] => Array 
     (
       [User] => Array 
        (
          [id] => 'the_auto_incrementing_id' 
          // other table columns 
          [created] 'creation_date' 
          [modified] 'modification_date' 
        ) 
       [School] => Array 
        (
          [id] => 'the_auto_incrementing_id' 
          // other table columns 
          [created] 'creation_date' 
          [modified] 'modification_date' 
        ) 
       [Country] => Array 
        (
          [id] => 'the_auto_incrementing_id' 
          // other table columns 
          [created] 'creation_date' 
          [modified] 'modification_date' 
        ) 
     ) 
) 

我的问题是,虽然我find('all')查询是在User模型发起的,是有可能orderBy假设模型School例如created场?

请让我知道这是不可能的

谢谢

回答

0

如果你正在试图获得来自用户,学校和国家所有相关的数据,但排序School.created(按你的例子),你可以运行学校模型中的查询。

我假设你在用户控制器是 - 如果是这样,只希望是这样的:

$myData = $this->User->School->find('all', array('order'=>'School.created DESC')); 

(无需加载学校的模式,因为它的链接, - 所以你的只是参考它通过用户模型)