简体   繁体   English

Yii不会加载模型

[英]Yii won't load a model

I am unable to load a model in my NewController . 我无法在NewController加载模型。 In other controllers I can perfectly call this line: 在其他控制器中,我可以完美地称呼这行:

$vid = Video::model()->findByPk($id);

and it will get me the right model. 它将为我提供正确的模型。

However in this controller: NewController.php it doesn't work. 但是在此控制器: NewController.php它不起作用。

public function actionUpdate($id)
{
    $model=$this->loadModel($id);

    $vid = Video::model()->findByPk($id);

    if(isset($vid)) // Check if it works.
       $this->render('update',array('model'=>$model, 'vid'=>$vid));
}

Does anyone have any suggestions why it doesn't work? 有人对它为什么不起作用有什么建议吗?

$id is valid and so is $model , I am going insane with this not working when it should. $id是有效的,因此$model也是如此,我应该为此疯狂而无法正常工作。


An example that works UserController.php : 一个工作UserController.php的示例:

public function actionView()
{
    if(isset($_GET['user']))
        $username = $_GET['user'];          
    else 
        $username = Yii::app()->user->name;

    $user = User::model()->find("username=:username",array(':username'=>$username));
    $saveQ = SavedQuery::model()->findAll("FK_userid=:id", array(':id'=>$user->id));

    $vid = Video::model()->findByPk($user->id);
    $this->render('View', array('user'=>$user, 'vid'=>$vid, 'saveQ'=>$saveQ));
}

You don't need make this: $vid = Video::model()->findByPk($id); 您不需要这样做: $vid = Video::model()->findByPk($id);

The actionUpdate load the data for update, the parameter $id seek the record to update 该actionUpdate负荷更新的数据,参数的$ id寻求记录更新

you are saying "The ID is the foreign key for the video. The integrity of the database is correct too." 您说的是“ ID是视频的外键。数据库的完整性也是正确的。” but $vid is taken using the primary key 但是$ vid是使用主键获取的

$vid = Video::model()->findByPk($id);

So you should probably have 所以你应该有

$vid = Video::model()->findAll("FK_XXXX=:id", array(':id'=>$id));

you need comment this: 您需要对此发表评论:

$model=$this->loadModel($id);

and try it. 尝试一下。

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

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