控制器无法在 yii 中插入通知 table
Controller cannot insert into notification table in yii
所以我有一个主要的 table comment
,它有很多字段。然后
每当将新记录插入 comment
table.
时,我想将一些字段插入 notifications
table
在控制器中,最初我有这个并且它适用于 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,
));
}
然后我为 notification
table 添加了更多行。但是没用。
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
table 的功能仍然有效。但是 notifications
table 没有。我试图重新排列位置,但什么也没发生。我也将 $notif->save();
更改为 $notif->insert();
但仍然没有任何反应。我错过了什么?
这是 table 结构:
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 ;
我在您的代码中找不到任何错误。
以下是我调试上述任务的假设
- 可能是
$_POST['Comment']['post_id']
没有提供值。
打印 Post 个值并检查您是否获得了所有必要的值。
print_r($_POST['Comment']);
在 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();
}
}
所以我有一个主要的 table comment
,它有很多字段。然后
每当将新记录插入 comment
table.
notifications
table
在控制器中,最初我有这个并且它适用于 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,
));
}
然后我为 notification
table 添加了更多行。但是没用。
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
table 的功能仍然有效。但是 notifications
table 没有。我试图重新排列位置,但什么也没发生。我也将 $notif->save();
更改为 $notif->insert();
但仍然没有任何反应。我错过了什么?
这是 table 结构:
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 ;
我在您的代码中找不到任何错误。
以下是我调试上述任务的假设
- 可能是
$_POST['Comment']['post_id']
没有提供值。 打印 Post 个值并检查您是否获得了所有必要的值。
print_r($_POST['Comment']);
在
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();
}
}