سؤال

In the form when I echo the session value it is prompted no errors, but when I try to save it on the database in the model the value is not saved.

Here is my view: (I just posted here what I think is needed.)

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

Here is my controller: The contentid, title and content are saved in the database only the userid is my problem. In my database I set the userid as 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));        
}

And here is my 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'),
    );
  }
}
هل كانت مفيدة؟

المحلول

Your user id Stored in Session it can accessed anywhere in the MVC Try this on your controller

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top