drop table if exists `tbl_user`;
CREATE TABLE tbl_user
(
`user_id` INTEGER NOT NULL AUTO_INCREMENT comment '主键',
`username` VARCHAR(128) NOT NULL comment '用户名',
`nickname` VARCHAR(128) NOT NULL comment '昵称',
`password` VARCHAR(128) NOT NULL comment '密码',
`email` VARCHAR(128) NOT NULL comment '邮箱',
`is_delete` tinyint not null default 0 comment '删除标志',
unique key(`username`),
primary key (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment='用户表';
drop table if exists `tbl_post`;
CREATE TABLE tbl_post
(
`post_id` INTEGER NOT NULL AUTO_INCREMENT comment '主键',
`title` VARCHAR(128) NOT NULL comment '标题',
`content` TEXT NOT NULL comment '文章内容',
`tags` TEXT comment '标签',
`status` INTEGER NOT NULL comment '状态,0 = 草稿,1 = 审核通过,-1 = 审核不通过,2 = 发布',
`create_time` INTEGER comment '创建时间',
`update_time` INTEGER comment '更新时间',
`author_id` INTEGER NOT NULL comment '作者',
`is_delete` tinyint not null default 0 comment '删除标志',
CONSTRAINT `post_ibfk_1` FOREIGN KEY (author_id)
REFERENCES tbl_user (`user_id`) ON DELETE CASCADE ON UPDATE RESTRICT,
primary key (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment='日志表';
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'post_id'),
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
);
}
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'post_id',
'title',
'content',
'tags',
'status',
'create_time',
'update_time',
'author_id',
'is_delete',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'post_id',
'title',
'content',
'tags',
'status',
'create_time',
'update_time',
'author_id',
'is_delete',
array(
'name'=>'is_delete',
'value'=>'is_delete?"是":"否"' //value 是可以执行 php 语句的哦
)
array(
'class'=>'CButtonColumn',
),
),
)); ?>
array(
'name'=>'is_delete',
'value'=>'is_delete?"是":"否"' //value 是可以执行 php 语句的哦
'filter' => array(0=>'否',1=>'是'), //自己定义搜索过滤的方式,这里为 是 和 否 的下拉菜单
'htmlOptions'=>array('class'=>'delete'), //可以定义 html 选项,这里是定义了带一个 delete 的类
),
array( 'header'=>'备注', 'value'=> 'display your data' ),
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'selectableRows' => 2, //允许多选,改为 0 时代表不允许修改,1 的话为单选
'class' => 'CCheckBoxColumn',//复选框
'headerHtmlOptions' => array('width'=>'18px'),//头部的 html 选项
'checkBoxHtmlOptions' => array('name' => 'myname','class'=>'myclass'), //复选框的 html 选项
),
'post_id',
'title',
'content',
'tags',
'status',
'create_time',
'update_time',
'author_id',
'is_delete',
array(
'name'=>'is_delete',
'value'=>'is_delete?"是":"否"', //value 是可以执行 php 语句的哦
'filter' => array(0=>'否',1=>'是'), //自己定义搜索过滤的方式,这里为 是 和 否 的下拉菜单
'htmlOptions'=>array('class'=>'delete'), //可以定义 html 选项,这里是定义了带一个 delete 的类
),
array(
'class'=>'CButtonColumn',
),
),
));
array(
'class'=>'ButtonColumn',
'template'=>"{view} {update}",
),
array(
'class'=>'ButtonColumn',
'template'=>"{view} {update} {print}",
'buttons'=>array(
'print'=>array(
'label'=>'打印',
'url'=>'Yii::app()->controller->createUrl("print", array("id"=>$data->post_id))',
'options'=>array("target"=>"_blank"),
),
),
),
$js = <<<_JS_
function(){
alert('The ajax finish');
}
_JS_;
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'afterAjaxUpdate'=>$js, //看这里,ajax 之后调用的 javascript 在这里....
'columns'=>array(
array(
'selectableRows' => 2, //允许多选,改为 0 时代表不允许修改,1 的话为单选
'class' => 'CCheckBoxColumn',//复选框
'headerHtmlOptions' => array('width'=>'18px'),
'checkBoxHtmlOptions' => array('name' => 'myname','class'=>'myclass'),
),
....
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'post_id'),
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
);
}
class Post extends CActiveRecord
{
public $name; //添加一个 public 属性,代表作者名
然后改一下 Model 里面 search 的代码,改动部分都已经加了注释:
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('author'); //添加了和 author 的渴求式加载
$criteria->compare('post_id',$this->post_id);
$criteria->compare('title',$this->title,true);
$criteria->compare('content',$this->content,true);
$criteria->compare('tags',$this->tags,true);
$criteria->compare('status',$this->status);
$criteria->compare('create_time',$this->create_time);
$criteria->compare('update_time',$this->update_time);
$criteria->compare('author_id',$this->author_id);
//这里添加了一个 compare, username 是 User 表的字段,$this->name 是我们添加的属性,true 为模糊搜索
$criteria->compare('username',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'post_id',
'title',
'content',
'tags',
'status',
'create_time',
'update_time',
'author_id',
/*下面就是添加的代码啊*/
array(
'name'=>'作者名称',
'value'=>'$data->author->username', //定义展示的 value 值
'filter'=>CHtml::activeTextField($model,'name'), //添加搜索 filter
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, content, status, author_id', 'required'),
array('status, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),
array('title', 'length', 'max'=>128),
array('tags', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('post_id, title, content, tags, status, create_time, update_time, author_id, name', 'safe', 'on'=>'search'),
);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有