drus40229 2015-09-03 04:52
浏览 30
已采纳

控制器无法插入到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 ;
  • 写回答

1条回答 默认 最新

  • douweng7083 2015-09-03 07:40
    关注

    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();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装