CGridView查询中 unsetAttributes() 方法详解
2016-01-13 00:00
435 查看
控制器代码:
public function actionIndex()
{
$model=new Staff('search');
if(isset($_GET['Manager']))
$model->attributes=$_GET['Manager'];
$this->render('index',array(
'model'=>$model,
));
}
将model传到CGridView,数据库有一条记录,但是却不显示。
CGridView代码部分如下:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'ajaxUpdate' => false,
'id'=>'staff-grid',
'itemsCssClass' => 'data_list',
'dataProvider'=>$model->search(),
'template' => '{items}{pager}',
'pager' => array(
'class'=>'LLinkPager',
),
'pagerCssClass' => 'myPager',
'enableSorting' => false,
'emptyText'=>'暂无数据',
'columns'=>array(
...
),
));
?>
对比默认生成的代码发现,在$model=new Staff('search');代码下有一行代码:$model->unsetAttributes(); 加上该代码后,发现数据可以显示。Why?
查看一下unsetAttributes() 方法
unsetAttributes() 方法(可用自 v1.1.3)
public void unsetAttributes(array $names=NULL)
$names array list of attributes to be set null.
If this parameter is not given, all attributes
as specified by attributeNames will have their values unset.
源码: framework/base/CModel.php#482 (隐藏)
public function unsetAttributes($names=null)
{
if($names===null)
$names=$this->attributeNames();
foreach($names as $name)
$this->$name=null;
}
该方法目的是将属性设置为NULL。
测试:
public function actionIndex()
{
$model=new Staff('search');
var_dump($model->attributes);
exit;
$model->unsetAttributes();
if(isset($_GET['Manager']))
$model->attributes=$_GET['Manager'];
$this->render('index',array(
'model'=>$model,
));
}
结果为:
array(6) {
["sector_id"]=>
string(1) "0"
["password"]=>
string(0) ""
["permission"]=>
int(0)
["logintime"]=>
int(0)
["id"]=>
NULL
["username"]=>
NULL
}
怎么sector_id值为0?原来,我在数据库中将sector_id的默认值设为0,是不是问题在这?于是,我将该字段默认值设置为2,刷新
结果为:
array(6) {
["sector_id"]=>
string(1) "2"
["password"]=>
string(0) ""
["permission"]=>
int(0)
["logintime"]=>
int(0)
["id"]=>
NULL
["username"]=>
NULL
}
原来如此。
因为数据库中的一条记录,它的sector_id的值为1,你以0查询,当然是找不到数据喽。
public function actionIndex()
{
$model=new Staff('search');
if(isset($_GET['Manager']))
$model->attributes=$_GET['Manager'];
$this->render('index',array(
'model'=>$model,
));
}
将model传到CGridView,数据库有一条记录,但是却不显示。
CGridView代码部分如下:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'ajaxUpdate' => false,
'id'=>'staff-grid',
'itemsCssClass' => 'data_list',
'dataProvider'=>$model->search(),
'template' => '{items}{pager}',
'pager' => array(
'class'=>'LLinkPager',
),
'pagerCssClass' => 'myPager',
'enableSorting' => false,
'emptyText'=>'暂无数据',
'columns'=>array(
...
),
));
?>
对比默认生成的代码发现,在$model=new Staff('search');代码下有一行代码:$model->unsetAttributes(); 加上该代码后,发现数据可以显示。Why?
查看一下unsetAttributes() 方法
unsetAttributes() 方法(可用自 v1.1.3)
public void unsetAttributes(array $names=NULL)
$names array list of attributes to be set null.
If this parameter is not given, all attributes
as specified by attributeNames will have their values unset.
源码: framework/base/CModel.php#482 (隐藏)
public function unsetAttributes($names=null)
{
if($names===null)
$names=$this->attributeNames();
foreach($names as $name)
$this->$name=null;
}
该方法目的是将属性设置为NULL。
测试:
public function actionIndex()
{
$model=new Staff('search');
var_dump($model->attributes);
exit;
$model->unsetAttributes();
if(isset($_GET['Manager']))
$model->attributes=$_GET['Manager'];
$this->render('index',array(
'model'=>$model,
));
}
结果为:
array(6) {
["sector_id"]=>
string(1) "0"
["password"]=>
string(0) ""
["permission"]=>
int(0)
["logintime"]=>
int(0)
["id"]=>
NULL
["username"]=>
NULL
}
怎么sector_id值为0?原来,我在数据库中将sector_id的默认值设为0,是不是问题在这?于是,我将该字段默认值设置为2,刷新
结果为:
array(6) {
["sector_id"]=>
string(1) "2"
["password"]=>
string(0) ""
["permission"]=>
int(0)
["logintime"]=>
int(0)
["id"]=>
NULL
["username"]=>
NULL
}
原来如此。
因为数据库中的一条记录,它的sector_id的值为1,你以0查询,当然是找不到数据喽。
相关文章推荐
- 同情快播?沉溺毛片是病,得治
- 基于xmpp openfire smack开发之Android客户端开发[3]
- 谍照:2016年3月,苹果要再次发布一系列“微创新”产品
- 马云:淘宝同星巴克一样是一种生活方式
- 京东包机送用户过年,作秀也请靠谱点
- iOS 9.3新功能前瞻:改善睡眠质量 让生活井井有条
- 45 万款应用里,最受欢迎的颜色竟然是……
- 阿里交互专家深度解析To B 和 To C产品设计差异
- 为什么很多产品经理都不愿意做后台?
- 程序员如何做到年薪超过60万?
- 是否应该立即将网站升级到Drupal 8?
- 为什么你不应该自行更新 Drupal 网站?
- PHP项目部署到阿里云,数据返回正常,http响应码500
- 销售数据分析之客户究竟集中在哪里?
- ceph 添加删除osd和mon
- Java基础知识总结(绝对经典)
- 修改PHP模板
- 获取app设置里通知是否开启
- OpenFire控制台输出日志
- 被选中的单选按钮