YiiFramework入门知识点总结(图文教程)(2)

public function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->password=md5($this->password); $this->create_user_id=Yii::app()->user->id;//====主要为此句。得到登陆帐号的ID $this->create_time=date('Y-m-d H:i:s'); } else { $this->update_user_id=Yii::app()->user->id; $this->update_time=date('Y-m-d H:i:s'); } return true; } else { return false; } }

更多相关:

/* 由于CComponent是post最顶级父类,所以添加getUrl方法。。。。如下说明: CComponent 是所有组件类的基类。 CComponent 实现了定义、使用属性和事件的协议。 属性是通过getter方法或/和setter方法定义。访问属性就像访问普通的对象变量。读取或写入属性将调用应相的getter或setter方法 例如: $a=$component->text; // equivalent to $a=$component->getText(); $component->text='abc'; // equivalent to $component->setText('abc'); getter和setter方法的格式如下 // getter, defines a readable property 'text' public function getText() { ... } // setter, defines a writable property 'text' with $value to be set to the property public function setText($value) { ... } */ public function getUrl() { return Yii::app()->createUrl('post/view',array( 'id'=>$this->id, 'title'=>$this->title, )); }

模型中的rules方法

/* * rules方法:指定对模型属性的验证规则 * 模型实例调用validate或save方法时逐一执行 * 验证的必须是用户输入的属性。像id,作者id等通过代码或数据库设定的不用出现在rules中。 */ /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('news_title, news_content', 'required'), array('news_title', 'length', 'max'=>128), array('news_content', 'length', 'max'=>8000), array('author_name, type_id, status_id,create_time, update_time, create_user_id, update_user_id', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, news_title, news_content, author_name, type_id, status_id, create_time, update_time, create_user_id, update_user_id', 'safe', 'on'=>'search'), ); }

说明:

1、验证字段必须为用户输入的属性。不是由用户输入的内容,无需验证。
2、数据库中的操作字段(即使是由系统生成的,比如创建时间,更新时间等字段——在boyLee提供的yii_computer源码中,对系统生成的这些属性没有放在safe中。见下面代码)。对于不是表单提供的数据,只要在rules方法中没有验证的,都要加入到safe中,否则无法写入数据库

yii_computer的News.php模型关于rules方法

/** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('news_title, news_content', 'required'), array('news_title', 'length', 'max'=>128, 'encoding'=>'utf-8'), array('news_content', 'length', 'max'=>8000, 'encoding'=>'utf-8'), array('author_name', 'length', 'max'=>10, 'encoding'=>'utf-8'), array('status_id, type_id', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, news_title, news_content, author_name, type_id, status_id', 'safe', 'on'=>'search'), ); }

视图中显示动态内容三种方法

1、直接在视图文件中以PHP代码实现。比如显示当前时间,在视图中:

复制代码 代码如下:

<?php echo date("Y-m-d H:i:s");?>


2、在控制器中实现显示内容,通过render的第二个参数传给视图

控制器方法中包含:

$theTime=date("Y-m-d H:i:s"); $this->render('helloWorld',array('time'=>$theTime));

视图文件:

复制代码 代码如下:

<?php echo $time;?>


调用的render()方法第二个参数的数据是一个array(数组类型),render()方法会提取数组中的值提供给视图脚本,数组中的 key(键值)将是提供给视图脚本的变量名。在这个例子中,数组的key(键值)是time,value(值)是$theTime则提取出的变量名$time是供视图脚本使用的。这是将控制器的数据传递给视图的一种方法。

3、视图与控制器是非常紧密的兄弟,所以视图文件中的$this指的就是渲染这个视图的控制器。修改前面的示例,在控制器中定义一个类的公共属性,而不是局部变量,它是值就是当前的日期和时间。然后在视图中通过$this访问这个类的属性。

视图命名约定

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/64fb5ccb0b5d6848244cdf37d7fe9431.html