在yii中使用mongodb扩展

1 篇文章 0 订阅

RT···用mongo记录日志体验下,发现并不像想象中那么顺畅

使用mongo的document对象存数数据,结构如下

{
    "_id" : "567d05ded569dbc10c0041b2",
    "order_id" : "1234",
}
然后在model的search方法里面写如下

[php]  view plain copy
  1. public function search()  
  2.    {  
  3.        // @todo Please modify the following code to remove attributes that should not be searched.  
  4.   
  5.        $criteria = new EMongoCriteria();  
  6.        $criteria->compare('order_id'$this->order_id);  
  7.        return new EMongoDataProvider($thisarray(  
  8.            'criteria'=>$criteria,  
  9.   
  10.        ));  
  11.    }  
死活查询不出数据,看 EMongoCriteria源码,在compare方法大概230行左右

[php]  view plain copy
  1. elseif(preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/'$value$matches)){  
  2.             $value = $matches[2];  
  3.             $op = $matches[1];  
  4.             if($partialMatch === true){  
  5.                 $value = new MongoRegex("/$value/i");  
  6.             }else{  
  7.                 ///* preg_match() Will only match real integers, unsigned */  
  8.                 // /* If it is a 64 bit system and the value is under the long max */  
  9.                 // /* value is under 32bit limit */  
  10.                 if(!is_bool($value) && !is_array($value) && preg_match('/^([0-9]|[1-9]{1}\d+)$/'$value) > 0  
  11.                     && ((PHP_INT_MAX > 2147483647 && (string)$value < '9223372036854775807') || (string)$value < '2147483647')){  
  12.                     $value = (int)$value;  
  13.                 }  
  14.             }  

犹豫拍板问题我把注释改造了下,大家可以看到 order_id 走到了 
[php]  view plain copy
  1. if(!is_bool($value) && !is_array($value) && p。。。。。这里 被强制转换成int了,而PHP中一般不会特别注意数据类型,order_id在插入的时候就写为了string类型,所以查不到,然后上面加个is_string方法即可。。以后空了在慢慢修改这个源码  
[php]  view plain copy
  1. 扩展地址为https://github.com/Sammaye/MongoYii.git  
PS:删除的无法恢复了。先凑合着。发现save()的时候保存的是字符串,但是查询order_id的时候类型是int,还是根据实际情况修改源码吧。建议插入的时候就做处理,我没做处理,只能在查询上做文章了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值