Php 多模态形式的yii博客评论模型
我想采用博客评论模型,并将表单转换为多模型表单,但我一直无法解决这个问题。如果有人能为我指出正确的方向,我将不胜感激 根据下面的设计,我想在注释中添加另一个表(OtherModel),并使用FK in注释链接这些表 控制器Php 多模态形式的yii博客评论模型,php,yii,Php,Yii,我想采用博客评论模型,并将表单转换为多模型表单,但我一直无法解决这个问题。如果有人能为我指出正确的方向,我将不胜感激 根据下面的设计,我想在注释中添加另一个表(OtherModel),并使用FK in注释链接这些表 控制器 public function actionView() { $post=$this->loadModel(); $comment=$this->newComment($post); $this->render('view',ar
public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
protected function newComment($post)
{
$comment=new Comment;
$otherModel=new OtherModel;
if(isset($_POST['Comment'], $_POST['OtherModel']))
{
$comment->attributes=$_POST['Comment'];
$otherModel->attributes=$_POST['OtherModel'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you...');
$this->refresh();
}
}
return $comment;
}
模型
通过CJuiTabs渲染表单
'Comment'=>$this->renderPartial('/comment/_form',array($model->$comment=>),true)
形式
带*的字段是必需的
//添加了otherModel作为MMF的一部分
我认为您在上面给出的代码中有一个错误:您试图使用$otherModel
变量,而不是$comment
方法所呈现的$actionView()
您是否尝试过使用YiFramework.com上发布的示例?我指的是你在评论你的问题时给出的链接。如果它不适合您,恐怕我误解了您的问题。我认为您在上面给出的代码中有一个错误:您试图使用
$otherModel
变量,而不是$comment
方法所呈现的$comment
您是否尝试过使用YiFramework.com上发布的示例?我指的是你在评论你的问题时给出的链接。如果它不适合您,恐怕我误解了您的问题。我认为您应该在保存之前验证这两个模型,否则您的数据库中会出现过时的$otherModel记录
protected function newComment($post)
{
$comment=new Comment;
$otherModel=new OtherModel;
if(isset($_POST['Comment'], $_POST['OtherModel']))
{
$comment->attributes=$_POST['Comment'];
$otherModel->attributes=$_POST['OtherModel'];
// also specify $otherModel as a param
if($post->addComment($comment, $otherModel))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you...');
$this->refresh();
}
}
return $comment;
}
public function addComment($comment, $otherModel)
{
// Validate both models, return false if there are errors.
// Errors should be available via $model->getErrors()
if ($comment->validate() && $otherModel->validate()) {
// save the otherModel first to obtain the generated ID
$otherModel->save();
$comment->other_id=$otherModel->id;
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
} else {
return false;
}
}
注意:您也可以在此处使用。我认为您应该在保存之前验证这两个模型,否则您的数据库中会出现过时的$otherModel记录
protected function newComment($post)
{
$comment=new Comment;
$otherModel=new OtherModel;
if(isset($_POST['Comment'], $_POST['OtherModel']))
{
$comment->attributes=$_POST['Comment'];
$otherModel->attributes=$_POST['OtherModel'];
// also specify $otherModel as a param
if($post->addComment($comment, $otherModel))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you...');
$this->refresh();
}
}
return $comment;
}
public function addComment($comment, $otherModel)
{
// Validate both models, return false if there are errors.
// Errors should be available via $model->getErrors()
if ($comment->validate() && $otherModel->validate()) {
// save the otherModel first to obtain the generated ID
$otherModel->save();
$comment->other_id=$otherModel->id;
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
} else {
return false;
}
}
注意:您也可以在此处使用。您是否介意更详细地解释您所问的问题,因为我不理解您关于“多模式”@aslingga含义的观点,例如您是否介意更详细地解释您所问的问题,因为我不理解您关于“多模式”@aslingga含义的观点,例如sainr,$otherModel是正确的,这是与注释模型相关的模型。这就是为什么这个问题是唯一的,我想同时收集Comment和OtherModel的值,使用Post控制器。好的,现在这个问题对我来说更清楚了。所以,可能在执行过程中发生了一些错误?我不确定为什么要使用
$otherModel->save()
在addComment
方法中,$otherModel
看起来像一个局部变量。sainr,希望能让你知道我想要什么。看看#2,我已经设置好了它,并且工作得很好(这是使用Post控制器和视图来创建新的注释)。现在的目标是能够使用相同的表单(/wwwroot/blog/protected/views/comment/_form.php),并使用相同的Post控制器和视图同时从两个模型(即comment和OtherModel)捕获数据。感谢您花时间研究此问题。我甚至还没有到执行点,因为当我将所需的表单输入从OtherModel(上面的名称和描述)添加到评论模型表单时,页面将不会呈现错误500。sainr,$OtherModel是正确的,这是与评论模型相关的模型。这就是为什么这个问题是唯一的,我想同时收集Comment和OtherModel的值,使用Post控制器。好的,现在这个问题对我来说更清楚了。所以,可能在执行过程中发生了一些错误?我不确定为什么要使用$otherModel->save()
在addComment
方法中,$otherModel
看起来像一个局部变量。sainr,希望能让你知道我想要什么。看看#2,我已经设置好了它,并且工作得很好(这是使用Post控制器和视图来创建新的注释)。现在的目标是能够使用相同的表单(/wwwroot/blog/protected/views/comment/_form.php),并使用相同的Post控制器和视图同时从两个模型(即comment和OtherModel)捕获数据。感谢您花时间来研究这一点。我甚至还没有到执行点,因为当我将所需的表单输入从OtherModel(上面的名称和描述)添加到评论模型表单时,页面将不会呈现错误500。
protected function newComment($post)
{
$comment=new Comment;
$otherModel=new OtherModel;
if(isset($_POST['Comment'], $_POST['OtherModel']))
{
$comment->attributes=$_POST['Comment'];
$otherModel->attributes=$_POST['OtherModel'];
// also specify $otherModel as a param
if($post->addComment($comment, $otherModel))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you...');
$this->refresh();
}
}
return $comment;
}
public function addComment($comment, $otherModel)
{
// Validate both models, return false if there are errors.
// Errors should be available via $model->getErrors()
if ($comment->validate() && $otherModel->validate()) {
// save the otherModel first to obtain the generated ID
$otherModel->save();
$comment->other_id=$otherModel->id;
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
} else {
return false;
}
}