简体   繁体   中英

Unknown Error in Yii Framework

I have this code in my Yii Music Store Application, but I didn't know the error(s). My idea is to show/browse the category/criteria of album, artist, and genre. But it always show "the else condition". I have tried to fix it but I can't find the error(s). The code is below. Hope anyone can fix it. Thanks all

<?php

class StoreController extends Controller
{
public $message;

public function actionIndex()
{
    $this->message = "Hello from Store.Index()";
    $this->render('index', array('content'=>$this->message));
}

public function actionBrowse()
{
    if(isset($_GET["gid"])){
        $gid = $_GET["gid"];

        $genreCriteria = new CDbCriteria();
        $genreCriteria->select = "`GenreId`, `Name`, `Description`";
        $genreCriteria->condition = "genreId = " . $_GET["gid"];

        $artistCriteria = new CDbCriteria();
        $artistCriteria->alias = "t1";
        $artistCriteria->select = "DISTINCT `t1`.`Name`, `t1`.`ArtistId`";
        $artistCriteria->join = "LEFT JOIN `tbl_album` ON `tbl_album`.`ArtistId` = `t1` . `ArtistId`";
        $artistCriteria->order = "`t1`.`ArtistId` ASC";

        $albumCriteria = new CDbCriteria();
        $albumCriteria->alias = "t2";
        $albumCriteria->select = "`AlbumId`, `GenreId`, `Title`, `Price`, `AlbumArtUrl`";
        $albumCriteria->condition = "`GenreId` = " . $_GET["gid"];
        $albumCriteria->order = "`ArtistId` ASC";

        $this->render('index', array('Albums' => Album::model()->findAll($albumCriteria),
                                     'Artist' => Artist::model()->findAll($artistCriteria),
                                     'Genre' => Genre::model()->findAll($artistCriteria)));
    }else{
        $this->message = "Hello from Store.Browse()";
        $this->render('index', array('content'=>$this->message));
    }
}

public function actionDetails()
{
    $this->message = "Hello from Store.Details()";
    $this->render('index', array('content'=>$this->message));
}

// Uncomment the following methods and override them if needed
/*
public function filters()
{
    // return the filter configuration for this controller, e.g.:
    return array(
        'inlineFilterName',
        array(
            'class'=>'path.to.FilterClass',
            'propertyName'=>'propertyValue',
        ),
    );
}

public function actions()
{
    // return external action classes, e.g.:
    return array(
        'action1'=>'path.to.ActionClass',
        'action2'=>array(
            'class'=>'path.to.AnotherActionClass',
            'propertyName'=>'propertyValue',
        ),
    );
}
*/
}

I'm pretty sure the answer is pretty simple, your page URL is missing "gid" and its value. This is the only reasonable explanation for "if isset" condition to fall under "else" section.

You can make a quick confirmation test. Go to your browser, enter www.yoursitename.com/store/browse?gid=111 and then see if it'll get the results you need. Keep in mind that i don't know your exact URL and its structure, so it's just an example, but the part "/ browse?gid=111 " is pretty important. It simple creates "gid" for $_POST array and sets its value equal to "111".

Additional note: together with "isset" you can also check up if "gid" value is empty or not, sometimes it's helpful and safer this way.

Also, here's my version of your Yii code:

$genreCriteria = new CDbCriteria();
    $genreCriteria->select = "GenreId, Name, Description";
    $genreCriteria->condition = "genreId = " . $_GET["gid"];

    $artistCriteria = new CDbCriteria();
    $artistCriteria->alias = "t1";
    $artistCriteria->select = "DISTINCT t1.Name, t1.ArtistId";
    $artistCriteria->join = "LEFT JOIN tbl_album ON tbl_album.ArtistId = t1.ArtistId";
    $artistCriteria->order = "t1.ArtistId ASC";

    $albumCriteria = new CDbCriteria();
    $albumCriteria->alias = "t2";
    $albumCriteria->select = "AlbumId, GenreI, Title, Price, AlbumArtUrl";
    $albumCriteria->condition = "GenreId = " . $_GET["gid"];
    $albumCriteria->order = "ArtistId ASC";

As you see, i simply removed quotes. This code wasn't tested locally in Yii, but the query itself - worked fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM