目錄
YII中model层的一个小问题。。
Jun 06, 2016 pm 08:45 PM
php
yii
下面是controller层的代码,用来保存表单数据:
<code>public function actionAdd(){ $categoryModel = new Category(); if(isset($_POST['Category'])){ $categoryModel->attributes = $_POST['Category']; if($categoryModel->save()){ $this->redirect(array('index')); } } </code>
登入後複製
登入後複製
我查看了CActiveRecord中的save方法是这样写的:
<code>public function save($runValidation=true,$attributes=null) { if(!$runValidation || $this->validate($attributes)) return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); else return false; } </code>
登入後複製
登入後複製
是我的理解的话应该是将$attribute传进去,但实际上并没有传进去。而且假设不传进去,也因该有:
<code>if($attributes==null){ $attributes=$this->attributes;} </code>
登入後複製
登入後複製
之类的啊!怎么连这个都没有?
回复内容:
下面是controller层的代码,用来保存表单数据:
<code>public function actionAdd(){ $categoryModel = new Category(); if(isset($_POST['Category'])){ $categoryModel->attributes = $_POST['Category']; if($categoryModel->save()){ $this->redirect(array('index')); } } </code>
登入後複製
登入後複製
我查看了CActiveRecord中的save方法是这样写的:
<code>public function save($runValidation=true,$attributes=null) { if(!$runValidation || $this->validate($attributes)) return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); else return false; } </code>
登入後複製
登入後複製
是我的理解的话应该是将$attribute传进去,但实际上并没有传进去。而且假设不传进去,也因该有:
<code>if($attributes==null){ $attributes=$this->attributes;} </code>
登入後複製
登入後複製
之类的啊!怎么连这个都没有?
谢邀。
题主有没有对 Category Model
使用 rule
规则,这涉及到了 Yii
中的 safe
机制,只有 Category
中的属性是 safe
时,才能通过 $categoryModel->attributes
接收到数据。请参考:
《Understanding "Safe" Validation Rules》
总结了一篇博文,题主可以看下: 《源码分析 Yii - save 机制》
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發
