简体   繁体   English

Yii框架中的未知错误

[英]Unknown Error in Yii Framework

I have this code in my Yii Music Store Application, but I didn't know the error(s). 我在Yii音乐商店应用程序中有这个代码,但我不知道错误。 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. 我很确定答案很简单,你的页面网址缺少“gid”及其价值。 This is the only reasonable explanation for "if isset" condition to fall under "else" section. 这是“if isset”条件属于“else”部分的唯一合理解释。

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. 转到您的浏览器,输入www.yoursitename.com/store/browse?gid=111 ,然后查看它是否能获得您需要的结果。 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. 请记住,我不知道您的确切URL及其结构,所以它只是一个示例,但“/ browse?gid = 111 ”部分非常重要。 It simple creates "gid" for $_POST array and sets its value equal to "111". 它简单地为$ _POST数组创建“gid”并将其值设置为“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. 附加说明:与“isset”一起,您还可以检查“gid”值是否为空,有时这样有用且更安全。

Also, here's my version of your Yii code: 另外,这是我的Yii代码版本:

$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. 这个代码没有在Yii本地测试,但查询本身 - 对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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