2014-04-29 43 views
0

在回顯會話值時,會提示沒有錯誤,但是當我嘗試將其保存在模型中的數據庫中時,該值不會保存。在yii中保存到數據庫的會話值

這是我的觀點:(我只是張貼在這裏我認爲需要)

<?php 
$userid = Yii::app()->session['iduser']; 
echo $userid; 
?> 

這裏是我的控制器: 的內容識別,標題和內容都保存在數據庫中唯一的用戶ID是我的問題。在我的數據庫中,我設定的用戶ID爲INT(11)NOT NULL

public function actionContent(){ 

$model=new ContentForm; 

    if(isset($_POST['ContentForm'])) { 
     $model->attributes=$_POST['ContentForm']; 
     if($model->save()) 
     $this->redirect(array('content','contentid'=>$model->contentid)); 
     $this->redirect(array('content','title'=>$model->title)); 
     $this->redirect(array('content','content'=>$model->content)); 
     $this->redirect(array('content','userid'=>$model->userid)); 
     } 

    $this->render('content',array('model'=>$model));   
} 

這裏是我的模型:在會話存儲

<?php 

    class ContentForm extends CActiveRecord{ 

public $content; 
public $title; 
public $userid; 

public function tableName(){ 
    return 'tbl_content'; 
} 

public function attributeLabels(){ 
    return array(
     'contentid' => 'contentid', 
     'content' => 'content', 
     'title' => 'title', 
     'userid' => 'userid', 
    ); 
} 

public function rules(){ 
    return array(
     array('content, title, userid', 'safe'), 
    ); 
    } 
} 

回答

2

您的用戶ID可以在MVC 任何地方訪問試試這個在你的控制器上

public function actionContent(){ 

$model=new ContentForm; 

    if(isset($_POST['ContentForm'])) { 
     $model->attributes=$_POST['ContentForm'];//The post values 
     $model->userid=Yii::app()->session['iduser']; 
     if($model->save()) 
     $this->redirect(array('content','contentid'=>$model->contentid)); 
     //$this->redirect(array('content','title'=>$model->title)); 
     //$this->redirect(array('content','content'=>$model->content)); //Why this to many redirects here the first redirect only works here 
     //$this->redirect(array('content','userid'=>$model->userid)); 
     } 

    $this->render('content',array('model'=>$model));   
}