1

I want to create an activeQuery using model::find()->where... that should generate sql like one below ( nevermind the logic ). No joins, just a lot of where conditions for single table. I guess it should be simple and I'm just missing somewhere.
I'm not able to figure out how to create such nested where conditions. Any hints?

select * from article where 
( 
  (site="domain.com" AND service="electronics" AND source="reader" AND tag like "%" AND marker like "%" ) 
  OR 
  (site="domain.net" AND service="electronics" AND source like "%" AND tag like "%" AND marker like "%" )
) 
AND NOT 
( 
  (site="domain.com" AND service="electronics" AND source like "%" AND tag like "%" AND marker like "%" ) 
  OR
  (site="domain.us" AND service="electronics" AND source like "%" AND tag like "%" AND marker like "%" ) 
)

3 Answers 3

2

You could try nesting it by using the 'and'and 'or' operators:

['and', 'type=1', 
    ['or', 'id=1', 'id=2']
] 

will generate
type=1 AND (id=1 OR id=2)

More info: https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail

Sign up to request clarification or add additional context in comments.

Comments

0

The simplest way could be using findBySql

this way

$myQuery = 'select * from article where 
( 
  (site="domain.com" AND service="electronics" AND source="reader" AND tag  like "%" AND marker like "%" ) 
  OR 
  (site="domain.net" AND service="electronics" AND source like "%" AND tag  like "%" AND marker like "%" )
) 
AND NOT 
( 
  (site="domain.com" AND service="electronics" AND source like "%" AND tag like "%" AND marker like "%" ) 
  OR
  (site="domain.us" AND service="electronics" AND source like "%" AND tag like "%" AND marker like "%" ) 
)';

$model = Article::findBySql($myQuery)->all(); 

Otherwise if you want Active Data Provider you can do this way

$dataProvider = new ActiveDataProvider([
   'query' => Article::findBySql($myQuery)->all(),
   'pagination' => [
       'pageSize' => 10,
   ],
   'sort' => [
     ........
    ],
]);

8 Comments

But if I do so I cannot use dataprovider and gridview. GridView won't be searchable/sortable when using findBySql . I'm using findBySql at the moment, but it would help me a lot if I could use AR.
Using myQuery as value for query you can do what you need (dataProvider). I have update the answer
But it won't work because for gridview I need SearchModel, and when using findBySql() "note that because the SQL statement is already specified, calling additional query modification methods (such as where(), order()) on the created " - ote that because the SQL statement is already specified, calling additional query modification methods (such as where(), order()) on the created .. "
I don't understand explain better. In DataProvider you set the query you prefer.the same you can do in search.(is Another dataprovider) In every situation you can use this kind of query is not o problem where you use it. If the query you show is what you need you can use it with findBySql in all method you need
but in GridView i need to use filterModel to be able to search and unless i start writing a lot of SQL I guess it won't work as it has to add ->andFilterWhere (if you take a look at any example generated by gii ) . and ->andFilterWhere won't work with findBySql. My question is how to create such query in AR or to know if this impossible, not to use findBySql as this is a bit different story .
|
0

Ok I've found the answer. I used Model::find()->where() and built nested arrays. This way I can use ActiveRecord and all gridview/dataprovider features.

1 Comment

I hate it when people accept their own answers without actually giving any details on the solution. At least add a code sample of the code you ended up with. Saying 'I found the solution` does not help anyone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.