Php Yii:ID在加载时被覆盖
我需要有关跟踪此web应用程序的帮助。我对Yii非常陌生,我正试图剖析一个现有的应用程序,以便更好地理解它。我正在尝试创建一个编辑函数,视频教程让我相信它与添加函数[Php Yii:ID在加载时被覆盖,php,yii,Php,Yii,我需要有关跟踪此web应用程序的帮助。我对Yii非常陌生,我正试图剖析一个现有的应用程序,以便更好地理解它。我正在尝试创建一个编辑函数,视频教程让我相信它与添加函数[save()]的过程完全相同,只是您指定了要覆盖的ID(我很可能在这一点上错了) 据我所知,以下文件正在使用中: views/forum/view.php views/forum/_commentform.php views/forum/_comments.php controllers/ForumController.php m
save()
]的过程完全相同,只是您指定了要覆盖的ID(我很可能在这一点上错了)
据我所知,以下文件正在使用中:
- views/forum/view.php
- views/forum/_commentform.php
- views/forum/_comments.php
- controllers/ForumController.php
- models/Forum.php
- models/Comment.php
view.php
开始,其中显示了很多内容。它的底部是:
<?php
$this->widget('zii.widgets.CListView',
array('dataProvider'=>$dataProvider, 'itemView'=>'_comments', 'summaryText'=>'',));
?>
该编辑按钮从数据库获取当前注释的ID。正如应用程序日志所告诉我的,这确实有效
在ForumController.php
中调用此特定函数:
public function actionEditComment() {
if(isset($_GET['reply'])) {
$comment=Comment::model()->findByAttributes(array('id'=>$_GET['reply']));
$topic=Forum::model()->findByAttributes(array('id'=>$comment->content_id));
$this->renderPartial('_commentform', array('forum'=>$topic, 'model'=>$comment, 'view'=>'view',));
}
}
接下来是\u commentform.php
。没什么,只是一个文本框,虽然它会检查是否有ID;如果是,它将编辑现有注释,否则,它将创建新注释。根据isNewRecord
的值,提交按钮也会从Reply
更改为Update
编辑:还有一个CActiveForm,以防有任何帮助。可能与路由有关
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'action'=>Yii::app()->createUrl('forum/view/id/'.$forum->id),
'enableAjaxValidation'=>false,
)); ?>
<?php
if ($view == 'view') {
echo CHtml::submitButton($model->isNewRecord ? 'Reply' : 'Update', array('id'=>'comment'.$model->id));
}?>
有趣的是,如果我将$comment
从newComment()
写到日志中,它确实会打印出编辑过的评论(即,如果我编辑了现有的评论“谁是冠军?”,它会打印出“john cena”),但是$comment->id
会产生一个空值,我认为这就是为什么不更新,编辑后的注释将另存为新注释
至于模型,Forum.php
和Comment.php
奇怪地指向同一个数据库表,因为出于某种原因,他们决定将论坛和评论放在一个表中Forum.php
还包含实际的addComment
函数(我觉得这个位置很奇怪),不过当流到达那里时,注释ID当然是空的,尽管编辑的注释本身就在那里
我哪里出错了?我错过什么了吗
编辑:以下是注释模型的属性和规则:
public function attributeLabels() {
return array(
'id' => 'ID',
'node_type' => 'Node Type',
'party_id' => 'Party',
'category' => 'Category',
'title' => 'Title',
'content' => 'Content',
'date_created' => 'Create Time',
'date_modified' => 'Update Time',
'status' => 'Status',);
}
public function rules()
{
/* combine parent and own rules */
$parentRules = parent::rules();
$myRules = array(
array('node_type_id', 'default', 'value'=>'7'), /* set type to Person */
array('node_type_id', 'in', 'range'=>array('7')), /* allow only Person type */
array('party_id, date_created, date_modified, status', 'numerical', 'integerOnly'=>true),
array('category, title, content', 'safe'),
);
/* you want to apply parent rules last, delete them here if necessary */
return array_merge($myRules);
}
你能在这里发表评论吗 我想您在Comment::rules()中没有“id”的规则, 当未定义属性规则时,属性将不安全,您不能通过$comment->attributes将其设置为值,或者您可以将代码更改为类似以下内容:
if(isset($_POST['Comment']) && isset($_POST['Comment']['id'])) {
$comment = Comment::model()->findByPk($_POST['Comment']['id']);
$comment->attributes=$_POST['Comment'];
$post->addComment($comment);
}
大约有一百行。我是否应该发布
属性标签()
?我还编辑了\u commentform.php
的一部分,这与CActiveForm有关,这可能是强制的。不,在这里发布CommentAs的just rules()方法,因为你没有id的规则。这是一个解决方案:数组('id,party\u id,date\u created,date\u modified,status','numeric','integerOnly'=>true),我得到一个未能在我的日志中设置“Comment”错误的不安全属性“content\u id”。从newComment($post)
打印出$post
将产生以下结果:2016/04/16 04:36:36[info][application]数组('node\u type\u id'=>'7''content\u id'=>'1''category'=>'CD Forum Local Testing''content'=>'Who is Champ?')
这意味着它正试图将评论设置为论坛,我认为,由于id被覆盖。
public function attributeLabels() {
return array(
'id' => 'ID',
'node_type' => 'Node Type',
'party_id' => 'Party',
'category' => 'Category',
'title' => 'Title',
'content' => 'Content',
'date_created' => 'Create Time',
'date_modified' => 'Update Time',
'status' => 'Status',);
}
public function rules()
{
/* combine parent and own rules */
$parentRules = parent::rules();
$myRules = array(
array('node_type_id', 'default', 'value'=>'7'), /* set type to Person */
array('node_type_id', 'in', 'range'=>array('7')), /* allow only Person type */
array('party_id, date_created, date_modified, status', 'numerical', 'integerOnly'=>true),
array('category, title, content', 'safe'),
);
/* you want to apply parent rules last, delete them here if necessary */
return array_merge($myRules);
}
if(isset($_POST['Comment']) && isset($_POST['Comment']['id'])) {
$comment = Comment::model()->findByPk($_POST['Comment']['id']);
$comment->attributes=$_POST['Comment'];
$post->addComment($comment);
}