控制器无法插入到yii的通知表中

控制器无法插入到yii的通知表中

问题描述:

So I have one primary table comment that has a number of fields. Then I want to insert some fields into a notifications table whenever a new record is inserted into comment table.

In the controller, initially I had this and it worked for comment table:

public function actionCreate() {
    $model = new Comment;
    if (isset($_POST['Comment'])) {
        $model->attributes = $_POST['Comment'];
        if ($model->save())
            $this->redirect(array('view', 'id' => $model->comment_id));
    }
    $this->render('create', array(
        'model' => $model,
    ));
}

Then I put some more lines for the notification table. But it didn't work.

public function actionCreate() {
    $model = new Comment;
    $notif = new Notifications;
    if (isset($_POST['Comment'])) {
        $model->attributes = $_POST['Comment'];
        $notif->peg = 'nofriani';
        $notif->tanggal = new CDbExpression("NOW()");
        $notif->notif = ' mengirimkan berita ';
        $notif->isi = $_POST['Comment']['post_id'];
        $notif->link = 'links';
        $notif->save();
        if ($model->save())
            $this->redirect(array('view', 'id' => $model->comment_id));
    }
    $this->render('create', array(
        'model' => $model,
    ));
}

The function for comment table still works. But the one for notifications table doesn't. I tried to rearrange the positions but nothing happened. I also changed the $notif->save(); into $notif->insert(); but still nothing happened. What have I missed?

This is the table structure:

CREATE TABLE IF NOT EXISTS notifications (
   id_notif int(11) NOT NULL AUTO_INCREMENT,
   tanggal date NOT NULL,
   peg varchar(30) NOT NULL,
   notif text NOT NULL,
   isi varchar(30) NOT NULL,
   link varchar(255) NOT NULL,
   PRIMARY KEY (id_notif)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

所以我有一个主表 comment code>,它有很多字段。 然后 我想在 comment code>表中插入新记录时将一些字段插入 notifications code>表。 p>

在控制器中 ,最初我有这个,它适用于 comment code>表: p>

  public function actionCreate(){
 $ model = new Comment; 
 if  (isset($ _ POST ['Comment'])){
 $ model-> attributes = $ _POST ['Comment']; 
 if($ model-> save())
 $ this->  redirect(array('view','id'=> $ model-> comment_id)); 
} 
 $ this-> render('create',array(
'model'=> $  model,
)); 
} 
  code>  pre> 
 
 

然后我为 notification code>表添加了更多行。 但它没有用。 p>

  public function actionCreate(){
 $ model = new Comment; 
 $ notif = new Notifications; 
 if(isset($)  _POST ['Comment'])){
 $ model-> attributes = $ _POST ['Comment']; 
 $ notif-> peg ='nofriani'; 
 $ notif-> tanggal = new CDbExpression  (“NOW()”); 
 $ notif-> notif ='mengirimkan berita'; 
 $ notif-> isi = $ _POST ['Comment'] ['post_id']; 
 $ notif-&gt  ; link ='links'; 
 $ notif-> save(); 
 if($ model-> save())
 $ this-> redirect(array('view','id'=  > $ model-> comment_id)); 
} 
 $ this-> render('create',array(
'model'=> $ model,
)); 
} 
   code>  pre> 
 
 

comment code>表的功能仍然有效。 但 notifications code>表的那个却没有。 我试图重新安排位置但没有发生任何事情。 我还将 $ notif-> save(); code>更改为 $ notif-> insert(); code>但仍未发生任何事情。 我错过了什么? p>

这是表结构: p>

  CREATE TABLE IF NOT EXISTS通知(
 id_notif int(11)  NOT NULL AUTO_INCREMENT,
 tanggal date NOT NULL,
 peg varchar(30)NOT NULL,
 notif text NOT NULL,
 isi varchar(30)NOT NULL,
 link varchar(255)NOT NULL,
  PRIMARY KEY(id_notif)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 3; 
  code>  pre> 
  div>

I could not find any mistakes in your code.

Bellow are my assumptions to debug above task

  1. May be $_POST['Comment']['post_id'] is not supplying the value.
  2. Print Post values and check whether you are getting all necessary values.

     print_r($_POST['Comment']);
    
  3. Validate $notif model before save(). It will display the validation errors if your model has.

    echo CActiveForm::validate($notif);
    

You can write above code in a better way as bellow.

    $model = new Comment;
    $notif = new Notifications;
    if (isset($_POST['Comment']))
    {
        $model->attributes = $_POST['Comment'];
        if ($model->validate() && $model->save())
        {
            $notif->peg = 'nofriani';
            $notif->tanggal = new CDbExpression("NOW()");
            $notif->notif = ' mengirimkan berita ';
            $notif->isi = $_POST['Comment']['post_id']; 
            $notif->link = 'links';                
            if($notif->validate() && $notif->save())
            {
                $this->redirect(array('view', 'id' => $model->comment_id));                    
            }       
            else
            {
                echo CActiveForm::validate($notif); 
                Yii::app()->end();
            }
        }
        else
        {
            echo CActiveForm::validate($model); 
            Yii::app()->end();
        }
    }