您的位置:首页 > 其它

andFilterWhere()函数找不出某个int类型字段为0的数据

2016-11-12 17:17 429 查看
首先本人刚接触YII2,  小知道都统计起来的。

andFilterWhere()函数找不出某个int类型字段为0的数据

$query = Equip::find()->andFilterWhere(['and','is_delete',0]);


该字段的值只有0和1。数据库中页确实存在该字段值为0的数据,但是这样写提示没有找到数据,把0改成1能找出该字段值为1的数据。如果不加andFilterWhere语句,那么会正常显示所有的数据,包括is_delete字段为0的数据。

先说解决办法, 改成下面的形式 :

$query = Equip::find()->andFilterWhere(['is_delete' => 0]);
或者
$query = Equip::find()->andFilterWhere(['and', ['is_delete' => 0]]);

接下来分析
为什么会出现这么有趣的现象

is_delete = 1
可以达到预期,
不加这个条件
也可以达到预期, 偏偏
is_delete = 0
有问题

原因是按照你写的那样, 最终生成的
sql
会是 :

select * from table where is_delete and 0 (或者1)
;

有意思的是,
select * from table where fieldName
这样的语句并不会报错.

我试了下,
fieldName
为空, 为null, 为0的时候, 查不到.

其他时候均能查到, 但是此种情况下, 不会用到索引.

yii 数据model->save()相关

$this->model = Customer::model();

$client = $_POST['client'];

$this->model->attributes 
   =$client;

$this->model->create_user    =$create_user;

$this->model->create_date    =$create_date;

$this->model->save();

$customer_id =$this->model->attributes['id']; //获取save插入后的id

上面是半年前写的,有不足,下面重新写着说明一下。

$customer =Customer::model();

$post_data = $_POST['client'];

$customer->attributes    =$post_data;

$customer->create_user    =Yii::app()->user->getId();

$customer->create_time    =time();

$customer->save();
$customer_id = $customer->id;  //这样获取save插入后的id最方便
$customer_id =$customer->attributes['id']; //这样也可以 获取save插入后的id
$customer_info = $customer->attributes;  //这样可以获取save插入后的相应model所有属性

直接 $this->model->id 不是更简单?
http://blog.sina.com.cn/s/blog_ea7f2ce40102wods.html        

yii model->save() 返回false

 yii model层操作总结 http://www.cnblogs.com/xieqian111/p/5212505.html

$model->save()执行时,如何不要验证?

[code]public boolean save(boolean $runValidation=true, array $attributes=NULL) $model->save(false); //不验证

1.按钮的id为btnzhuce

==》 控制按钮为禁用:

$("#btnzhuce").attr({"disabled":"disabled"});

==》控制按钮为可用

 $("#btnzhuce").removeAttr("disabled");//将按钮可用
http://blog.csdn.net/dc769319/article/details/53022570    不修改源文件  直接 刷新验证码
http://www.yiichina.com/tutorial/410 http://www.360us.net/article/17.html    验证码的使用
https://segmentfault.com/a/1190000005910783  重写源生验证码

先设置
$model->allow_comment = 1


<?= $form->field($model, 'allow_comment')->checkbox([ 'label' => '允许评论']) ?>


这样这默认选中了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐