Cakephp automagic並不實際連接表,正如你可以在底部的佈局中的sql_dump中看到的那樣。
你會看到下面的查詢。
SELECT `Player`.`id`, `Player`.`name`, `Player`.`created` FROM `players` AS `Player` WHERE `Player`.`id` = 10 GROUP BY `Player`.`id`
SELECT `Score`.`id`, `Score`.`player_id`, `Score`.`scores` FROM `scores` AS `Score` WHERE `Score`.`player_id` = (10)
很明顯,它不是真正的連接表,所以你需要按照以下方式做。
以下將幫助你,因爲我已經測試過它。
在播放器控制器中,您需要首先解除分數模型,然後自定義代碼以加入分數表,如下所示。
在玩家模型中創建一個虛擬字段,如下所示。
<?php
class Player extends AppModel
{
var $name = 'Player';
var $displayField = 'name';
var $virtualFields = array
(
'Distance' => 'SUM(Score.scores)'
);
var $hasMany = array
(
'Score' => array
(
'className' => 'Score',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>
爲了測試我手動添加conditions
找到玩家人數10 scores
<?php
class PlayersController extends AppController {
var $name = 'Players';
function index()
{
$this->Player->unbindModel(array
(
'hasMany' => array
(
'Score'
)
));
$this->Player->bindModel(array
(
'belongsTo' => array
(
'Score' => array
(
'foreignKey' => false,
'conditions' => array
(
'Player.id = Score.player_id'
)
)
)
));
$order = "Player.id";
$direction = "asc";
if(isset($this->passedArgs['sort']) && $this->passedArgs['sort']=="Distance")
{
$order = $this->passedArgs['sort'];
$direction = $this->passedArgs['direction'];
unset($this->passedArgs['sort']);
unset($this->passedArgs['direction']);
}
$this->pagination = array();
$this->pagination = array
(
'conditions' => array
(
'Score.player_id' => 10
),
'fields' => array
(
'Player.*',
'SUM(Score.scores) AS Distance'
),
'group' => array
(
'Score.player_id'
),
'limit' => 15,
'order'=>$order." ".$direction
);
$playersScore = $this->paginate('Player');
}
}
?>
,所得陣列將類似於下。
Array
(
[0] => Array
(
[Player] => Array
(
[id] => 10
[name] => Dexter Velasquez
[created] => 2012-08-02 12:03:07
[Distance] => 18
)
)
)
對於測試我已使用Generate Mysql Data。
爲如下排序上面的鏈接鏈接
<?php $direction = (isset($this->passedArgs['direction']) && isset($this->passedArgs['sort']) && $this->passedArgs['sort'] == "Distance" && $this->passedArgs['direction'] == "desc")?"asc":"desc";?>
和顯示。
<?php echo $this->Paginator->sort('Distance','Distance',array('direction'=>$direction));?>
cakephp實際上查找相關表並將數據追加到數組。所以你需要手動強制cakephp解除綁定舊的關係和bindModel連接...因爲只要沒有加入表,你不能在查詢中使用其他表字段....希望你明白我的意思。 –
謝謝。它工作正常。但我怎樣才能用分頁組件做到這一點?並按距離排序。 (或其他列) – mrdaliri
您不必擔心分頁問題,因爲每個分頁頁面都會執行此代碼,因此您的數據足夠精確 –