yii-blog 学习笔记[二]

上接第一部分:
mailstone 2. blog 管理。
自定义post 模型.
首先:两个配置块.
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('content title', 'required'),
array('status, author_id', 'numerical', 'integerOnly'=>true),
array('title', 'length', 'max'=>50),
array('tags', 'length', 'max'=>255),
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/' , 'message'=>'Tags can only contain word characters!'),
// array('tags' , 'normalizeTags'),
array('content, update_time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, content, tags, status' , 'safe', 'on'=>'search'),
);
}

//常用rules 写法。
array('content title', 'required'),   //必填项
array('status, author_id', 'numerical', 'integerOnly'=>true),  //整形
array('title', 'length', 'max'=>50), //最大长度限制
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/' , 'message'=>'Tags can only contain word characters!'),

array('email' , 'email'),  //邮件规则。
array('url' , 'url'),  //url 规则。

// array('tags' , 'normalizeTags'), //自定义方法处理。array('title,status', 'safe', 'on'=>'search'), //场景情况处理,以后详细研究。
//追查一下yii 的源代码,会在createValidators 里面用过。更多细节,后期学习。

2 relations()

yii 的 active record 支持 relation ,简称 rar  ,通过配置相应的 relation model 可以调用其对应的关系数据。 常见的类型有  BELONGS_TO 和 HAS_MANY   还有STAT  统计
举例 posts 内容表的  author_id  为外健,对应关联用户表
一个内容对应多条 回复 comments  则为has_many
内容对应的 commentcount 为统计,为评论的条数。



//belongto关系 关联model  外健名
'author'=>array(self::BELONGS_TO , 'User' , 'author_id') , 
// has many 关系 对应模型,模型的外健名 ,其它查询条件。这里 comment 模型为定义 const STATUS_APPROVED =1 ,和对应的排序条件. 'comments' => array( self::HAS_MANY , 'Comment' , 'post_id' , 'condition' =>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),

对应模型,模型的外健名 ,,其它查询条件。这里 comment 模型为定义 const STATUS_APPROVED =1 ,和对应的排序条件
//统计字段 commentCount 对应的模型
'commentCount'=>array(self::STAT , 'Comment' , 'post_id' , 'condition'=>'status='.Comment::STATUS_APPROVED)//使用方法如何. $post 为model$author=$post->author;echo $author->username;$comments=$post->comments;foreach($comments as $comment){ echo $comment->content ;//...}//值得多说一点的是 这里配置了后,调用时,会再次依据关系的配置查询数据库,在列表数据时,其性能将会极差,如何直接在查询时使用视图查询呢? 这个是有几种方法,通常在查询时,设置好with 即可,更多参阅手册。



将列表字段中的status 显示文字!
//视图界面中

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'content',
'tags',
array(
'name'=>'status',
'value'=>'Lookup::item("PostStatus",$data->status)',
'filter'=>Lookup::items('PostStatus'),
),
'create_time',
/*
'update_time',
'author_id',
*/
array(
'class'=>'CButtonColumn',
),
),





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值