1
我試圖將以下查詢轉換爲數據提供者,因此它可以顯示在CGridView中。我曾嘗試使用CArrayDataProvider,但至今沒有任何運氣,任何幫助將不勝感激!將自定義sql查詢轉換爲數據提供者yii?
下面是該查詢
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
$query = "SELECT t.*,
(SELECT COUNT(*)
FROM tbl_competition_teams ct
WHERE ct.team = t.id
AND ct.competition = :competitionId) AS 'inCompetition'
FROM tbl_teams t
WHERE t.level = :levelId";
$params = array(
'levelId' => $levelId,
'competitionId' => $competitionId
);
$result = array();
$teams = $this->findAllBySQL($query, $params);
return $teams;
}
這是我試圖使它在一個CArrayDataProvider:
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
$rawData = Yii::app()->db->createCommand("SELECT t.*,
(SELECT COUNT(*)
FROM tbl_competition_teams ct
WHERE ct.team = t.id
AND ct.competition = :competitionId) AS 'inCompetition'
FROM tbl_teams t
WHERE t.level = :levelId")->queryAll();
$params = array(
'levelId' => $levelId,
'competitionId' => $competitionId
);
return new CArrayDataProvider($rawData, array(
'id'=>'id',
'sort'=>array(
'attributes'=>array(
'id', 'title', 'club', 'level', 'inCompetition',
),
),
)); }
但這給了我錯誤「CDbCommand未能執行SQL語句:SQLSTATE [HY093]:無效的參數編號:沒有參數被綁定。」
這是我的球隊表
CREATE TABLE `tbl_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(35) NOT NULL DEFAULT '',
`level` int(10) unsigned DEFAULT NULL,
`club` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2952 ;
和我競爭的球隊表
CREATE TABLE `tbl_competition_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`competition` int(10) unsigned NOT NULL DEFAULT '0',
`team` int(10) unsigned NOT NULL DEFAULT '0',
`seasonId` int(11) NOT NULL DEFAULT '3',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=126320 ;
在此先感謝一百萬的任何建議!
感謝,它現在就像$ RAWDATA =的Yii ::應用程序() - > DB-> createCommand(「SELECT T *, \t \t(SELECT COUNT(*)FROM CT tbl_competition_teams WHERE CT。 .team = t.id AND ct.competition =」 $ competitionId。 ')AS inCompetition \t \t FROM tbl_teams噸 \t \t WHERE t.level =' $ levelId) - > queryAll(); – user1180888