public function actionCountListManageByCategories() { $accountId = $this->getAccountId(); $keyword = $this->getParams("keyword", ''); if ($keyword == "undefined") { $keyword = ''; } $active = $this->getParams("active", 'Y'); $categories = $this->getParams('categories', []); $restaurantNames = []; $types = []; for ($i = 0; $i < count($categories); $i++) { if ($categories[$i]['name'] == '餐廳') { $restaurantNames = $categories[$i]['items']; } else { $types = array_merge($types, $categories[$i]['items']); } } $listCount = 0; $query = new Query(); $query->from('uhkklpCookbook'); if ($active == 'Y') { $query->where(['active' => 'Y'])->andWhere(['startDate' => ['$ne' => null], 'startDate' => ['$lte' => time()]])->andWhere(['endDate' => ['$ne' => null], 'endDate' => ['$gte' => time()]]); } else { $query->where(['or', ['startDate' => null], ['startDate' => ['$gt' => time()]], ['endDate' => null], ['endDate' => ['$lt' => time()]], ['active' => 'N']]); } $query->andWhere(['isDeleted' => false, 'hasImportImg' => true])->andWhere(['accountId' => $accountId])->andWhere(['or', ['like', 'title', $keyword], ['like', 'ingredient.name', $keyword]]); if (count($restaurantNames) > 0) { $sql = array('like', 'restaurantName', $restaurantNames[0]); if (count($restaurantNames) > 1) { $sql = array('or'); for ($i = 0; $i < count($restaurantNames); $i++) { array_push($sql, array('like', 'restaurantName', $restaurantNames[$i])); } } $query->andWhere($sql); } if (count($types) > 0) { $sql = array('like', 'type', $types[0]); if (count($types) > 1) { $sql = array('or'); for ($i = 0; $i < count($types); $i++) { array_push($sql, array('like', 'category', $types[$i])); } } $query->andWhere($sql); } $listCount = $query->count(); Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['code' => 200, 'msg' => 'OK', 'result' => $listCount]; }
public function testWhere() { $query = new Query(); $query->where(['name' => 'name1']); $this->assertEquals(['name' => 'name1'], $query->where); $query->andWhere(['address' => 'address1']); $this->assertEquals(['and', ['name' => 'name1'], ['address' => 'address1']], $query->where); $query->orWhere(['name' => 'name2']); $this->assertEquals(['or', ['and', ['name' => 'name1'], ['address' => 'address1']], ['name' => 'name2']], $query->where); }