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

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab