0

I am builing an app with yii and I tried doing my sql calls the "pretty way" using the yii query builder the same way seen on this guide

http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

And this is my built query:

$diseaseCountSqlQuery = Yii::app()->db->createCommand()
                                    ->select ('tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count')
                                    ->from ('tbl_disease')
                                    ->join ('tbl_symptom_disease', 'ICD10=diseaseCode')
                                    ->where ($symptomsOrQueryArray)
                                    ->group ('ICD10')
                                    ->queryAll();
            //placeholder

            $maxDiseaseCountQuery = Yii::app()->db->createCommand()
                                    ->select ('MAX(disease_count) AS max_disease_count')
                                    ->from ($diseaseCountSqlQuery)
                                    ->queryAll();
            //multiple symptom query
            $diseaseCodes = Yii::app()->db->createCommand()
                            ->select ('ICD10')
                            ->from ($maxDiseaseCountQuery)
                            ->join ($diseaseCountSqlQuery, 'max_disease_count=disease_count')
                            ->queryAll();

The problem appears in the second command specifically the line:

->from ($diseaseCountSqlQuery)

I get an "strpos() expects parameter 1 to be string, array given " error.

I can solve my problem by just copying the entire mysql command into a string and then using create command, but I wanted to use this more "elegant" way. Can someone help me fix it and explain a bit how subqueries work with query builder? Thank you :)

2
  • the error tells you exactly what the problem is: you're trying to use an array where a string is expected. you'd need to do a foreach($diseaseCountSqlQuery as $part) { ...->from($part)} or whatever. Commented Jun 18, 2014 at 18:09
  • ok, so is there a way to use the result table of the sql command in a different query? Commented Jun 18, 2014 at 18:15

1 Answer 1

1

You can't create subqueries like this. The first queryAll returns an array with results. You can't query this like it's a database. The from() part expects a parameter in the form of a string, not an array.

Try it like this:

Yii::app()->db->createCommand('complete query, including subquery')->queryAll();

Or use PHP to filter the results after executing the first query.

More info on createCommand: http://www.yiiframework.com/doc/api/1.1/CDbConnection#createCommand-detail http://www.yiiframework.com/doc/api/1.1/CDbCommand

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

2 Comments

Do you have any useful resources so I can understand the use of createCommand that way better?
Take a look at the links I've added to the answer.

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.