【问题标题】:Session value saved to database in yiiyii 中保存到数据库的会话值
【发布时间】:2023-04-09 23:52:01
【问题描述】:

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

这是我的看法: (我只是在这里发布了我认为需要的内容。)

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

这是我的控制器: contentid、title 和 content 都保存在数据库中,只有 userid 是我的问题。在我的数据库中,我将用户 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'),
    );
  }
}

【问题讨论】:

    标签: php yii parameter-passing session-variables


    【解决方案1】:

    您的用户 id 存储在 Session 中,它可以在 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));        
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多