簡體   English   中英

cakephp由關聯中的另一個模型字段排序

[英]cakephp order by another model field in association

關於cakeBy中orderBy的一個快速問題,僅在PHP中查找操作。 假設我有3個相互關聯的模型。 當我在我的3個模型中的任何一個上執行find('all') cakePHP查詢時,我都會得到結果,其中還包括來自其他2個模型的數據。 例如,假設我的模型是:

1- User
2- School
3- Country

如果我在UsersController執行$this->find('all') ,因為我的三個模型鏈接在一起,我將得到如下所示:

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例如,假設模型Schoolcreated字段是?

請告訴我是否有可能

謝謝

如果您試圖從“用戶”,“學校”和“國家/地區”獲取所有相關數據,但按School.created排序(根據您的示例),則可以從School模型中運行查詢。

我假設您在“用戶”控制器中-如果是這樣,那就是這樣:

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

(由於它是鏈接的,因此無需加載學校模型,因此您只需通過用戶模型來引用它即可)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM