繁体   English   中英

会话值以yii格式保存到数据库中

[英]Session value saved to database in yii

在我回显会话值的形式中,它提示没有错误,但是当我尝试将其保存在模型中的数据库时,不保存该值。

以下是我的观点:(我刚刚发布了我认为需要的内容。)

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

这是我的控制器:contentid,标题和内容都保存在数据库中,只有用户ID是我的问题。 在我的数据库中,我将userid设置为int(11)而不是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'),
    );
  }
}

您的用户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));        
}

暂无
暂无

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

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