所以我連接我的兩個模型在一起,它的返回是這樣的:檢索來自兩個聯接的表
Array
(
[Submission] => Array
(
[id] => 47
[user_id] => 0
[title] => asdfasdfsa dfasdf asdfa sfa fadf
[source] => http://www.aol.com
[slug] =>
[category] => health
[created] => 2012-06-25 11:30:16
)
[User] => Array
(
[id] => 2
[username] => john
)
[SubmissionsVote] => Array
(
[0] => Array
(
[id] => 247
[user_id] => 2
[submission_id] => 47
[vote_type] => up
)
)
)
然而,有時票的數量將是[0] - [N ]。這是我的$newestSubmissions
。我做給我查詢是:
public function newestSubmissions() {
$this->unBindModel(
array('hasMany' => array('Comment')));
return $this->find('all', array(
'fields' => array(
'Submission.id',
'Submission.user_id',
'Submission.title',
'Submission.source',
'Submission.slug',
'Submission.category',
'Submission.created',
'User.id',
'User.username'
),
'order' => 'Submission.created DESC'
));
}
我想什麼做的是整個$newestSubmissions
數組對象檢索所有的票以及所有的票(以下查詢)上的每一個在$newestSubmissions
計算實際得分,所以我可以發送被叫$newestSubmissions
我的看法單個陣列對象,而不是$newestSubmissions
和$submissionScore
public function getVoteType($userId, $submissionId) {
$voteType = $this->find('all', array(
'conditions' => array(
'User.id' => $userId,
'Submission.id' => $submissionId),
'fields' => array(
'SubmissionsVote.vote_type'),
'limit' => '1'
));
return $voteType[0]['SubmissionsVote']['vote_type'];
}
基本上我想要一個單一的分數發送到我的觀點與所有其他信息一起而不必發送內部索引數組obje ct在我的父數組對象(這是我目前正在獲取)。
我該怎麼做?
謝謝我會研究一下。這看起來更好的解決方案。 – 2012-08-11 19:12:57