Search code examples
mysqlyiidataprovider

Converting custom sql query in to a dataprovider yii?


I'm trying to convert the following query in to a dataprovider, so it can be displayed within a CGridView. I have tried using a CArrayDataProvider, but have not had any luck so far, any help would be much appreciated!

Here is the query

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;
}

This is how I tried to make it in to a 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',
    ),
),

)); }

but that gives me the error "CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound."

Here is my teams table

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 ;

and my competition teams table

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 ;

Thanks a million in advance for any advice!


Solution

  • Maybe the problem is you queryAll() execute the query. At this momento you are not assigning the values to you parameters

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

    also you can try to rewrite your query.

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