{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/10053","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/10053/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/10053/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/10053/events","html_url":"https://github.com/yiisoft/yii2/issues/10053","id":114095562,"number":10053,"title":"Multiple \"andFilterWhere\" getting nested instead of appending conditions","user":{"login":"XzAeRo","id":1656037,"avatar_url":"https://avatars.githubusercontent.com/u/1656037?v=3","gravatar_id":"","url":"https://api.github.com/users/XzAeRo","html_url":"https://github.com/XzAeRo","followers_url":"https://api.github.com/users/XzAeRo/followers","following_url":"https://api.github.com/users/XzAeRo/following{/other_user}","gists_url":"https://api.github.com/users/XzAeRo/gists{/gist_id}","starred_url":"https://api.github.com/users/XzAeRo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/XzAeRo/subscriptions","organizations_url":"https://api.github.com/users/XzAeRo/orgs","repos_url":"https://api.github.com/users/XzAeRo/repos","events_url":"https://api.github.com/users/XzAeRo/events{/privacy}","received_events_url":"https://api.github.com/users/XzAeRo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-29T16:49:50Z","updated_at":"2015-10-29T17:59:21Z","closed_at":null,"body":"I have multiple range inputs in a custom filter form. In the search model I correctly split the values for querying the database. Eg:\r\n```\r\nlist($timestamp_since, $timestamp_to) = explode(';', $this->daterange);\r\n$query->andFilterWhere(['between', 'timestamp', $timestamp_since, $timestamp_to]);\r\n```\r\nAnd this generates the correct SQL query:\r\n\r\n```\r\nSELECT * FROM `table` WHERE `timestamp` BETWEEN '2015-10-28 00:00' AND '2015-10-28 23:59' ORDER BY `timestamp` DESC LIMIT 10\r\n```\r\n\r\nBut when I do this:\r\n```\r\n// date range\r\nlist($timestamp_since, $timestamp_to) = explode(';', $this->daterange);\r\n$query->andFilterWhere(['between', 'timestamp', $timestamp_since, $timestamp_to]);\r\n\r\n// attribute 1 range\r\nlist($bottom, $top) = explode(';',$this->attribute1_range);\r\n$query->andFilterWhere([$this->attribute1_notbetween? 'not between':'between', 'attribute1', $bottom, $top]);\r\n\r\n// attribute 2 range\r\nlist($bottom, $top) = explode(';', $this->attribute2_range);\r\n$query->andFilterWhere([$this->attribute2_notbetween? 'not between':'between', 'attribute2', $bottom, $top]);\r\n```\r\n\r\nGenerates this SQL query:\r\n```\r\nSELECT * FROM `table` WHERE ((`timestamp` BETWEEN  '2015-10-28 00:00' AND '2015-10-28 23:59') AND (`attribute1` BETWEEN '0' AND '40')) AND (`attribute2` BETWEEN '-20' AND '50') ORDER BY `timestamp` DESC LIMIT 10\r\n```\r\n\r\nWhich starts nesting the \"AND\" conditions further and further as I add more filters.\r\n\r\nI even tried merging everything in a single call, like:\r\n\r\n```\r\n$query->andFilterWhere([\r\n    [$this->attribute1_notbetween? 'not between':'between', 'attribute1', $attribute1_range_bottom, $attribute1_range_top],\r\n    [$this->attribute2_notbetween? 'not between':'between', 'attribute2', $attribute2_range_bottom, $attribute2_range_top],\r\n]);\r\n```\r\n\r\nBut still generates the same output.\r\n\r\nThe docs didn't help that much either. because the filter examples are for exact queries (`WHERE a=1`...)\r\n\r\nThanks in advance.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/152266783","html_url":"https://github.com/yiisoft/yii2/issues/10053#issuecomment-152266783","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/10053","id":152266783,"user":{"login":"mikehaertl","id":675062,"avatar_url":"https://avatars.githubusercontent.com/u/675062?v=3","gravatar_id":"","url":"https://api.github.com/users/mikehaertl","html_url":"https://github.com/mikehaertl","followers_url":"https://api.github.com/users/mikehaertl/followers","following_url":"https://api.github.com/users/mikehaertl/following{/other_user}","gists_url":"https://api.github.com/users/mikehaertl/gists{/gist_id}","starred_url":"https://api.github.com/users/mikehaertl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikehaertl/subscriptions","organizations_url":"https://api.github.com/users/mikehaertl/orgs","repos_url":"https://api.github.com/users/mikehaertl/repos","events_url":"https://api.github.com/users/mikehaertl/events{/privacy}","received_events_url":"https://api.github.com/users/mikehaertl/received_events","type":"User","site_admin":false},"created_at":"2015-10-29T17:59:21Z","updated_at":"2015-10-29T17:59:21Z","body":"The SQL is equivalent, so what is the problem? As the AND operator is commutative, you can set as many brackets as you like between the operands. There's no difference in the query result."}}