gpt4 book ai didi

php - Controller 无法插入 yii 中的通知表

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:26 24 4
gpt4 key购买 nike

所以我有一个包含多个字段的主表 comment。然后每当将新记录插入到 comment 表中时,我想将一些字段插入到 notifications 表中。

在 Controller 中,最初我有这个并且它适用于 comment 表:

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,
));
}

然后我为 notification 表添加了一些行。但它没有用。

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,
));
}

comment 表的功能仍然有效。但是 notifications 表没有。我试图重新排列位置,但什么也没发生。我还将 $notif->save(); 更改为 $notif->insert(); 但仍然没有任何反应。我错过了什么?

这是表结构:

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. 可能是 $_POST['Comment']['post_id'] 没有提供值。
  2. 打印 Post 值并检查您是否获得了所有必要的值。

     print_r($_POST['Comment']);
  3. save() 之前验证 $notif 模型。如果您的模型有,它将显示验证错误。

    echo CActiveForm::validate($notif);

你可以用下面这样更好的方式编写上面的代码。

    $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();
}
}

关于php - Controller 无法插入 yii 中的通知表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com