《PHP編程:PHP的Yii框架的基本使用示例》要點(diǎn):
本文介紹了PHP編程:PHP的Yii框架的基本使用示例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:YII框架
在 Yii 自動(dòng)生成的代碼里,我們總能在 admin 的界面看到 CGridView 的身影.這是一個(gè)很好用的展示數(shù)據(jù)的表格控件,用的好可以明顯地加快開發(fā)進(jìn)度.下面就讓我們來(lái)探索一下 CGridView 的基本使用吧:PHP應(yīng)用
???? 簡(jiǎn)單起見,我們的代碼就用 Yii demo 中的 blog 例子來(lái)做修改.首先,這是修改后的部分 Mysql 語(yǔ)句:PHP應(yīng)用
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 '刪除標(biāo)志', 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 '標(biāo)題', `content` TEXT NOT NULL comment '文章內(nèi)容', `tags` TEXT comment '標(biāo)簽', `status` INTEGER NOT NULL comment '狀態(tài),0 = 草稿,1 = 審核通過(guò),-1 = 審核不通過(guò),2 = 發(fā)布', `create_time` INTEGER comment '創(chuàng)建時(shí)間', `update_time` INTEGER comment '更新時(shí)間', `author_id` INTEGER NOT NULL comment '作者', `is_delete` tinyint not null default 0 comment '刪除標(biāo)志', 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='日志表';
??? 兩個(gè)表一個(gè)存儲(chǔ)作者信息一個(gè)存儲(chǔ)日志,其中日志有一個(gè)外鍵關(guān)聯(lián)到 user.兩個(gè)表里面的 is_delete 字段是標(biāo)志該條記錄是否被刪除,0 為未刪除,1 為已刪除.讓我們看一下用 gii 生成的 Post 類的 relation 方法:PHP應(yīng)用
/** * @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'), ); }
??? 其中的 author 外鍵作為 BELONGS_TO 關(guān)系存在,符合我們的預(yù)期.
??? 說(shuō)了這么多,看看自動(dòng)生成的 Post 中 admin.php 里 CGridView 的代碼吧:PHP應(yīng)用
<?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', ), ), )); ?>
??? 看!雖然我們什么都沒(méi)寫,但這就是這個(gè)控件的最基礎(chǔ)使用了.dataProvider 是由 model 里面的 search 函數(shù)提供的數(shù)據(jù),filter...暫時(shí)看不出這里的作用,columns 控制展示的每一列,其中最后一項(xiàng)的 CButtonColumn 向我們展示了三個(gè)按鈕,分別是 查看? 更新 和 刪除.
??? 接下來(lái)我們一點(diǎn)點(diǎn)地改造.PHP應(yīng)用
用 CGridView 展示我們真正要的數(shù)據(jù)形式:
??? 很多時(shí)候,數(shù)據(jù)庫(kù)里的東西不適合直接展示給用戶看,需要我們進(jìn)行一定的處理之后才適合閱讀.但在這里不經(jīng)修改的話 CGridView 只會(huì)把數(shù)據(jù)庫(kù)的值原封不動(dòng)地呈現(xiàn),所以,我們應(yīng)該在相應(yīng)的字段進(jìn)行修改.比如 is_delete 字段,數(shù)據(jù)庫(kù)里存放的是 0 和 1,但是在這里閱讀就不太好了,我們應(yīng)該改成 1 展示 '是' ,0展示 '否'.看看下面的代碼,我們用了一個(gè) array,兩個(gè)鍵分別是 name 和 value,name 對(duì)應(yīng)的要填寫該 model 擁有的字段,而 value 是你想展示的數(shù)據(jù),這里可以寫成一個(gè) php 語(yǔ)句,作為可以執(zhí)行的代碼.看到這里,是不是覺(jué)得對(duì)這個(gè) value 我們可以做很多東西?有的同學(xué)可能要問(wèn),如果我想執(zhí)行的代碼很長(zhǎng),難道都寫在 value 里面?...我說(shuō)同學(xué),你不會(huì)在其他地方寫成一個(gè)函數(shù)然后在這里調(diào)用它嗎??PHP應(yīng)用
<?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 是可以執(zhí)行 php 語(yǔ)句的哦 ) array( 'class'=>'CButtonColumn', ), ), )); ?>
???? 除此之外,還有一些常用的選項(xiàng),都可以在 array 里面填寫,下面是比較常見的使用方式(其他部分代碼省略):PHP應(yīng)用
array( 'name'=>'is_delete', 'value'=>'is_delete?"是":"否"' //value 是可以執(zhí)行 php 語(yǔ)句的哦 'filter' => array(0=>'否',1=>'是'), //自己定義搜索過(guò)濾的方式,這里為 是 和 否 的下拉菜單 'htmlOptions'=>array('class'=>'delete'), //可以定義 html 選項(xiàng),這里是定義了帶一個(gè) delete 的類 ),
???? 上面我們用 name 的話那是 model 里原來(lái)就有的字段,如果我們想展示自己定義的新內(nèi)容呢,用 header :PHP應(yīng)用
array( 'header'=>'備注', 'value'=> 'display your data' ),
添加 CCheckBoxColumn :
??? 有時(shí)也許我們會(huì)需要一個(gè)復(fù)選框,來(lái)對(duì)每一行進(jìn)行選擇,這時(shí),我們可以增加一列,用 CCheckBoxColumn 類:PHP應(yīng)用
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'post-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( array( 'selectableRows' => 2, //允許多選,改為 0 時(shí)代表不允許修改,1 的話為單選 'class' => 'CCheckBoxColumn',//復(fù)選框 'headerHtmlOptions' => array('width'=>'18px'),//頭部的 html 選項(xiàng) 'checkBoxHtmlOptions' => array('name' => 'myname','class'=>'myclass'), //復(fù)選框的 html 選項(xiàng) ), 'post_id', 'title', 'content', 'tags', 'status', 'create_time', 'update_time', 'author_id', 'is_delete', array( 'name'=>'is_delete', 'value'=>'is_delete?"是":"否"', //value 是可以執(zhí)行 php 語(yǔ)句的哦 'filter' => array(0=>'否',1=>'是'), //自己定義搜索過(guò)濾的方式,這里為 是 和 否 的下拉菜單 'htmlOptions'=>array('class'=>'delete'), //可以定義 html 選項(xiàng),這里是定義了帶一個(gè) delete 的類 ), array( 'class'=>'CButtonColumn', ), ), ));
修改ButtonColumn:
?? 注意到列表每一項(xiàng)的最后三個(gè)小圖標(biāo)嗎?不需要的話當(dāng)然是直接刪了,那要是只要其中某幾個(gè)呢?可以加一個(gè) template 參數(shù):PHP應(yīng)用
array( 'class'=>'ButtonColumn', 'template'=>"{view} {update}", ),
??? 也可以自定義按鈕:PHP應(yīng)用
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"), ), ), ),
刷新時(shí)觸發(fā) Javascript:
???? 如果你想在每次搜索之后觸發(fā)一些 Javascript ,Yii 也提供了這個(gè)選項(xiàng),你只要寫成一個(gè)函數(shù)然后設(shè)置 afterAjaxUpdate 就好,記住這只是在 ajax 請(qǐng)求完成之后調(diào)用的,如果你想在 頁(yè)面 一開始加載完成就調(diào)用的話需要另外加到頁(yè)面的? JavascriptPHP應(yīng)用
$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 之后調(diào)用的 javascript 在這里.... 'columns'=>array( array( 'selectableRows' => 2, //允許多選,改為 0 時(shí)代表不允許修改,1 的話為單選 'class' => 'CCheckBoxColumn',//復(fù)選框 'headerHtmlOptions' => array('width'=>'18px'), 'checkBoxHtmlOptions' => array('name' => 'myname','class'=>'myclass'), ), ....
?添加 關(guān)聯(lián)表 相關(guān)字段的搜索:
???? 先說(shuō)一句,我們?cè)谶@里只談 ”一對(duì)多“ 的關(guān)聯(lián)搜索,首先,不要忘了我們的數(shù)據(jù)庫(kù),忘記的同學(xué)請(qǐng)戳這里:這里,可以看到在 tbl_post 中是有一個(gè)外鍵關(guān)聯(lián)到 tbl_user 表的,用以查找作者的相關(guān)信息.建了數(shù)據(jù)庫(kù)之后,看看我們生成的 Yii 代碼的 POST 的 Model, 里面的 realtion 如下(忽略 comment 的):PHP應(yīng)用
/** * @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'), ); }
??? 可以看到 POST 和 USER 表可以通過(guò) author 鍵進(jìn)行訪問(wèn),例如: $model->author->nickname,而且 這里是 BELONGS_TO 關(guān)系.
??? 說(shuō)了這么多,我們的需求究竟是什么?....PHP應(yīng)用
???? 產(chǎn)品經(jīng)理推了推眼鏡:”我們要在日志的后臺(tái)管理界面加一個(gè)功能,可以通過(guò)作者名稱搜索到相應(yīng)的文章.這個(gè)比較急,今晚就要完成.“PHP應(yīng)用
??? 淡定淡定,不就是改需求嗎.忽略進(jìn)度要求,我們研究一下究竟要做什么.
??? 其實(shí)很簡(jiǎn)單的,不就是在 POST 的 admin 界面中增加一列作者名稱,然后可以通過(guò)作者名的 模糊搜索 去找到對(duì)應(yīng)日志嗎?看看代碼,要是通過(guò) 作者 id 去搜索不就簡(jiǎn)單了嗎?不過(guò)這樣確實(shí)不太友好...如果是展示作者名字而已不也是很簡(jiǎn)單嗎?加一個(gè) header 然后 value 是 $data->author->username, 問(wèn)題是這樣只能展示,不能進(jìn)行搜索...哎,好苦惱.
??? 淡定淡定,不就是多個(gè)搜索嗎?來(lái),讓我告訴你怎么做.PHP應(yīng)用
??? 首先,我們進(jìn)入 POST 的 model,在一開始的地方添加一個(gè)屬性:PHP應(yīng)用
class Post extends CActiveRecord { public $name; //添加一個(gè) public 屬性,代表作者名 然后改一下 Model 里面 search 的代碼,改動(dòng)部分都已經(jīng)加了注釋: 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); //這里添加了一個(gè) compare, username 是 User 表的字段,$this->name 是我們添加的屬性,true 為模糊搜索 $criteria->compare('username',$this->name,true); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); }
??? 然后在 view 里面,就是 post 文件夾的 admin.php ,CGridView 改為下面代碼:PHP應(yīng)用
<?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', ), ), )); ?>
??? 你是不是發(fā)現(xiàn)現(xiàn)在有了搜索框但是不起作用呢?哈哈,所以我們說(shuō)文章要堅(jiān)持看到最后.我們要做的最后一步,就是在 rule 里面,把 name 屬性加入到安全搜索字段中,要不然會(huì)被 Yii 認(rèn)為是不安全字段而過(guò)濾掉的.看,就在下面函數(shù)的最后一行,safe 前面多了個(gè) name ....PHP應(yīng)用
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'), ); }
《PHP編程:PHP的Yii框架的基本使用示例》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP編程:PHP的Yii框架的基本使用示例》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/8781.html