zoukankan      html  css  js  c++  java
  • yii2 使用mongo查询(包含like查询)

    基础查询model 类

    <?php
    
    namespace appmodels;
    
    
    use MongoDBBSONRegex;
    use MongoDBBSONUTCDateTime;
    use yiimongodbActiveRecord;
    /**
     * Class PlatformPayOrigins
     * @package appmodels
     */
    class MongoDb extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function getDb()
        {
            return Yii::$app->get('mongodb');
        }
    
        public static function collectionName()
        {
            return 'job_log';
        }
    
        /**
         * 获取列表
         * @param string $id
         * @param string $startTime
         * @param string $endTime
         * @param int $limit
         * @param bool $error
         * @return array|ActiveRecord
         * @date 2021/3/9
         * @time 17:05
         */
        public  function getList($id='',$startTime='',$endTime='',$limit=100,$error = true)
        {
            $where = [];
            if (!empty($id)) {
                $where['jobId'] = $id;
            }
    
            $items = self::find()->where($where)->select(['_Id','...','...']);
            if($error){
                //包含的数据 
           //mysql: select * from table where keyword like "%keyword%"
    if (!empty($where)) { $items->andWhere(['like','keyword','keyword')]); } else { $items->where(['like','keyword','keyword')]); } } if (!empty($startTime)) {
           // mysql:select * from table where beginTime >='2021-03-10'
    if (!empty($where)) { $items->andWhere(['>=', 'beginTime', new UTCDateTime($startTime)]); } else { $items->where(['>=', 'beginTime', new UTCDateTime($startTime)]); } } if (!empty($endTime)) { if (!empty($where)) { $items->andWhere(['<=', 'endTime', new UTCDateTime($endTime)]); } else { $items->where(['<=', 'endTime', new UTCDateTime($endTime)]); } } return $items->limit($limit) ->orderBy('endTime desc') ->asArray() ->all(); } }

    使用

            $model = new MongoDb();
            $model->getList('',$startTime,$endTime);    
    参考  

    https://www.yiiframework.com/extension/mongoyii



  • 相关阅读:
    大话设计模式--中介者模式
    大话设计模式--职责链模式
    大话设计模式--命令模式
    大话设计模式--桥接模式
    迷宫求解
    stuct、class、typedef
    软件测试
    Scrapy初探
    python练习
    链表基础
  • 原文地址:https://www.cnblogs.com/zhanzy/p/14509844.html
Copyright © 2011-2022 走看看