崩盘砖家
- 崩盘砖家 回复了话题 yii 对象释放内存问题
SecurityUser::model()
没超出范围,还是php的内容
这样写是把生成的 object放到了SecurityUser类中作为静态成员变量,
不管你调用多少次SecurityUser::model()
返回的始终是同一个对象
这个对象是永远存在的,除非你写一个相反的静态函数去destroy这个静态成员变量
看CActiveRecord类的model函数就清楚了public static function model($className=__CLASS__) { if(isset(self::$_models[$className])) return self::$_models[$className]; else { $model=self::$_models[$className]=new $className(null); $model->_md=new CActiveRecordMetaData($model); $model->attachBehaviors($model->behaviors()); return $model; } }
- 崩盘砖家 回复了话题 表单除了错误提示信息,怎么添加正常情况下的信息?
如果为了方便的话可以使用setFlash
当然也有其他方式,比如对model设置一个状态或消息字段 - 崩盘砖家 回复了话题 yii如何调用存储过程,求大能解答啊!!
一样的3
Yii::app()->getDb()->createCommand("call procedureName()")->execute()
http://www.yiiframework.com/doc/guide/1.1/en/database.ar#defining-ar-class
AR relies on well defined primary keys of tables. If a table does not have a primary key, it is required that the corresponding AR class specify which column(s) should be the primary key by overriding the primaryKey() method as follows, public function primaryKey() { return 'id'; // For composite primary key, return an array like the following // return array('pk1', 'pk2'); }
getPrimaryKey() method public mixed getPrimaryKey() {return} mixed the primary key value. An array (column name=>column value) is returned if the primary key is composite. If primary key is not defined, null will be returned. Source Code: framework/db/ar/CActiveRecord.php#1213 (show) public function getPrimaryKey() { $table=$this->getMetaData()->tableSchema; if(is_string($table->primaryKey)) return $this->{$table->primaryKey}; else if(is_array($table->primaryKey)) { $values=array(); foreach($table->primaryKey as $name) $values[$name]=$this->$name; return $values; } else return null; } Returns the primary key value.
5.2不会出问题,
两个同名的类不会冲突,因为import目录是将目录加入php的include path里面,用到的时候按照include path的顺序逐个搜索
要指定专用哪个model,可以Yii::import('aaa.bbb.YourModel',true)进行立即require,
看手册上import的导入顺序说明更清楚class UserRegForm extends CFormModel { public function rules() { //定义注册专用规则 } public function reg(){ //调用User类方法reg插入数据库 } } public function actionReg() { $user = new UserRegForm; if (isset($_POST['UserRegForm'])) { $user->attributes = $_POST['UserRegForm']; if ($user->validate()) { //如果表单验证成功 $user->reg(); } } }
大体上是这个思路,我习惯了不把表单验证放到ar类里面,不从User类继承,因为场景一多,我总感觉不好控制
两种方式:
1.如果是通过继承CFormModel,在类中定义一个reg方法,在控制器中当调用validate通过就调用reg();reg函数中实际上是new了一个user对象
2.RegisterForm直接从User模型继承,这样reg方法就放到user中- 崩盘砖家 回复了话题 关于网站前后台的第二种制作方法
正好在查这个,学习了
ding!!!!!!!!!!!!!!!!!!!!!!!