下面是controller层的代码,用来保存表单数据:
public function actionAdd(){
$categoryModel = new Category();
if(isset($_POST['Category'])){
$categoryModel->attributes = $_POST['Category'];
if($categoryModel->save()){
$this->redirect(array('index'));
}
}
我查看了CActiveRecord中的save方法是这样写的:
羊驼 v3.8 企业版(仿阿里巴巴单企业界面)方便中小企业建站使用,包含产品、新闻、网上询价等模块。羊驼! 是一个开源的轻量级树状 CMS 系统。 它基于 php + mysql 并以 b2core MVC 为底层架构。 可以方便快速的配置出个人、企业网站。在B2Core 优雅的MVC架构之上你可以轻松定制任意应用型网站。 羊驼 v3.8 企业版更新:修复了后台错误,和前台图片地址问题。 企业版主
0
public function save($runValidation=true,$attributes=null)
{
if(!$runValidation || $this->validate($attributes))
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
else
return false;
}
是我的理解的话应该是将$attribute传进去,但实际上并没有传进去。而且假设不传进去,也因该有:
if($attributes==null){
$attributes=$this->attributes;}
之类的啊!怎么连这个都没有?
下面是controller层的代码,用来保存表单数据:
public function actionAdd(){
$categoryModel = new Category();
if(isset($_POST['Category'])){
$categoryModel->attributes = $_POST['Category'];
if($categoryModel->save()){
$this->redirect(array('index'));
}
}
我查看了CActiveRecord中的save方法是这样写的:
public function save($runValidation=true,$attributes=null)
{
if(!$runValidation || $this->validate($attributes))
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
else
return false;
}
是我的理解的话应该是将$attribute传进去,但实际上并没有传进去。而且假设不传进去,也因该有:
if($attributes==null){
$attributes=$this->attributes;}
之类的啊!怎么连这个都没有?
谢邀。
题主有没有对 Category Model 使用 rule 规则,这涉及到了 Yii 中的 safe 机制,只有 Category 中的属性是 safe 时,才能通过 $categoryModel->attributes 接收到数据。请参考:
《Understanding "Safe" Validation Rules》
总结了一篇博文,题主可以看下: 《源码分析 Yii - save 机制》
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号