2014-02-18 106 views
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 ; 

在此先感謝一百萬的任何建議!

回答

4

也許問題是你queryAll()執行查詢。在這個時代報給你參數

$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(); 

也可以嘗試重寫查詢你不分配值。

$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"; 

$command= Yii::app()->db->createCommand($query); 
$command->bindValue(':levelId', $levelId); 
$command->bindValue(':competitionId', $competitionId); 
$rawData = $command->queryAll(); 
+0

感謝,它現在就像$ 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