楼主是从ThinkPHP转过来的,感觉TP的方法很好用,现在把他封装一下
首先修改项目的控制器基类 项目/protected/components/Controller.php
class Controller extends CController
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
//public $layout='//layouts/column1';
public $layout=false;
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu=array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
/**
* 成功提示
* @param type $msg 提示信息
* @param type $jumpurl 跳转url
* @param type $wait 等待时间
*/
public function success($msg="",$jumpurl="",$wait=3){
self::_jump($msg, $jumpurl, $wait, 1);
}
/**
* 错误提示
* @param type $msg 提示信息
* @param type $jumpurl 跳转url
* @param type $wait 等待时间
*/
public function error($msg="",$jumpurl="",$wait=3){
self::_jump($msg, $jumpurl, $wait, 0);
}
/**
* 最终跳转处理
* @param type $msg 提示信息
* @param type $jumpurl 跳转url
* @param type $wait 等待时间
* @param int $type 消息类型 0或1
*/
public function _jump($msg="",$jumpurl="",$wait=3,$type=0){
//生成URL地址
if(is_array($jumpurl)){
$jumpurl = $this->U($jumpurl[0],$jumpurl[1]);
}elseif($jumpurl){
$jumpurl = $this->U($jumpurl);
}
$data = array(
'msg' => $msg,
'jumpurl' => $jumpurl,
'wait' => $wait,
'type' => $type
);
$data['title'] = ($type==1) ? "提示信息" : "错误信息";
if(empty($jumpurl)){
if($type==1){
$data['jumpurl']=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:"javascript:window.close();";
}else{
$data['jumpurl'] = "javascript:history.back(-1);";
}
}
$this->renderPartial("//sys/template",array('data'=>$data));
}
/*
* 根据参数拼成路由
* @param string actionName方法名
* @param array $param 参数
*/
public function U($actionName,$param=''){
if($param){
return $this->createUrl($actionName, $param);
}else{
return $this->createUrl($actionName);
}
}
/**
* Ajax方式返回数据到客户端
* @access public
* @param mixed $data 要返回的数据
* @param String $type AJAX返回数据格式
* @return void
*/
public function ajaxReturn($data, $type = 'JSON') {
switch (strtoupper($type)) {
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit($this->xml_encode($data));
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
default :
// 其他返回格式抛出异常
exit('该数据格式尚未支持,请修改本函数源码添加对应的头');
}
}
/*
* 数据库的M方法
* @param string $sql sql语句
* @return object
*/
public function M($sql=''){
$obj = Yii::app()->db->createCommand($sql);
return $obj;
}
/*
* 数据库的M方法
* @param string $modelName 模型名
* @param int $type 0=增 or 1=删改查
* @return object
*/
public function D($modelName,$type=1){
if($type){
$model = call_user_func(array($modelName,model));
}else{
$model = new $modelName();
}
return $model;
}
/*
* dump方法
* @param $data 打印的数据
*/
public function dump($data){
echo '<pre>';
var_dump($data);
echo '</pre>';
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>跳转提示</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16px; }
.system-message{ width:500px;height:100px; margin:auto;border:6px solid #999;text-align:center; position:relative;top:50px;}
.system-message legend{font-size:24px;font-weight:bold;color:#999;margin:auto;width:100px;}
.system-message h1{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
.system-message .jump{ padding-right:10px;height:25px;line-height:25px;font-size:14px;position:absolute;bottom:0px;left:0px;background-color:#e6e6e1 ; display:block;width:490px;text-align:right;}
.system-message .jump a{ color: #333;}
.system-message .success,.system-message .error{ line-height: 1.8em; font-size: 15px }
.system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px; display:none}
</style>
</head>
<body>
<fieldset class="system-message">
<legend>提示信息</legend>
<div style="text-align:left;padding-left:10px;height:75px;width:490px; ">
<?php if($data['type']):?>
<p class="success">恭喜^_^!~<?php echo($data['msg']); ?></p>
<?php else:?>
<p class="error">Sorry!~<?php echo($data['msg']); ?></p>
<?php endif;?>
<p class="detail"></p>
</div>
<p class="jump">
页面自动 <a id="href" href="<?php echo($data['jumpurl']); ?>">跳转</a> 等待时间: <b id="wait"><?php echo($data['wait']); ?></b>
</p>
</fieldset>
<div style="clear: both"></div>
<script type="text/javascript">
(function(){
var wait = document.getElementById('wait'),href = document.getElementById('href').href;
totaltime=parseInt(wait.innerHTML);
var interval = setInterval(function(){
var time = --totaltime;
wait.innerHTML=""+time;
if(time === 0) {
location.href = href;
clearInterval(interval);
};
}, 1000);
})();
</script>
</body>
</html>
class ArctypeController extends Controller{
public function actionindex(){
echo $this->U('arctype/add',array('id'=>5));//U方法
$this->ajaxReturn(array('title'=>'标题','type'=>'栏目'));//ajaxReturn方法之前不能有输出
$this->success("写入数据成功",array('arctype/add',array('id'=>20,'name'=>'title')),3);//success方法
$this->error("写入数据成功",array('arctype/add',array('id'=>20,'name'=>'title')),3);//error方法
$this->dump($this->M('select id,typename from {{arctype}}')->queryAll());//M方法一
$this->dump($this->M()
->from('{{archive}} arc')
->select('arc.id,arc.title,type.typename')
->join('{{arctype}} type','arc.typeid=type.id')
->queryAll());//M方法二
$this->dump($this->D('Arctype')->findAll());//D方法删改查 第二个参数为0增
}
多谢老爱的提醒,使用call_user_func(array($modelName,model)); 替代$modelName::model()的方式,所以D方法得以成功实行