Hi YII experts,
Can you please tell me how can I write join queries in YII framework
here I wrote using Criteria, but the script is throwing me an error in the constructor part of the CDbCriteria.
here is my code
$Criteria = new CDbCriteria;
$Criteria->join = 'LEFT JOIN tests ON tests.id = 1';
$Criteria->join = 'LEFT JOIN test_section ON test_section.test_id = tests.id';
$Criteria->join = 'LEFT JOIN test_sections ON test_section.test_section_id = test_sections.id';
$Criteria->join = 'LEFT JOIN test_section_questions ON test_section_questions.test_id = tests.id';
$Criteria->addCondition = "test_sections.id = test_section_questions.test_section_id AND questions.id = test_section_questions.question_id";
$Criteria->group = 'GROUP BY questions.id';
$dataProvider=new CActiveDataProvider('Questions', array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>50,
),
));
I am getting the following error
PHP Error
Description
Invalid argument supplied for foreach()
Source File
C:\xampp\www\framework\db\schema\CDbCriteria.php(102)
00090: public $with;
00091: /**
00092: * @var string the alias name of the table. If not set, it means the alias is 't'.
00093: */
00094: public $alias;
00095:
00096: /**
00097: * Constructor.
00098: * @param array criteria initial property values (indexed by property name)
00099: */
00100: public function __construct($data=array())
00101: {
00102: foreach($data as $name=>$value)
00103: $this->$name=$value;
00104: }
00105:
00106: /**
00107: * Appends a condition to the existing {@link condition}.
00108: * The new condition and the existing condition will be concatenated via the specified operator
00109: * which defaults to 'AND'.
00110: * The new condition can also be an array. In this case, all elements in the array
00111: * will be concatenated together via the operator.
00112: * This method handles the case when the existing condition is empty.
00113: * After calling this method, the {@link condition} property will be modified.
00114: * @param mixed the new condition. It can be either a string or an array of strings.
Stack Trace
#0 C:\xampp\www\framework\web\CActiveDataProvider.php(75): CDbCriteria->__construct()
#1 C:\xampp\www\framework\base\CComponent.php(152): CActiveDataProvider->setCriteria()
#2 C:\xampp\www\framework\web\CActiveDataProvider.php(56): CActiveDataProvider->__set()
#3 C:\xampp\www\gmat3\protected\controllers\TestsController.php(248): CActiveDataProvider->__construct()
#4 C:\xampp\www\gmat3\protected\controllers\TestsController.php(189): TestsController->linearTest()
#5 C:\xampp\www\framework\web\actions\CInlineAction.php(32): TestsController->actionStart()
#6 C:\xampp\www\framework\web\CController.php(300): CInlineAction->run()
#7 C:\xampp\www\framework\web\filters\CFilterChain.php(129): TestsController->runAction()
#8 C:\xampp\www\framework\web\filters\CFilter.php(41): CFilterChain->run()
#9 C:\xampp\www\framework\web\CController.php(993): CAccessControlFilter->filter()
#10 C:\xampp\www\framework\web\filters\CInlineFilter.php(59): TestsController->filterAccessControl()
#11 C:\xampp\www\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter()
#12 C:\xampp\www\framework\web\CController.php(283): CFilterChain->run()
#13 C:\xampp\www\framework\web\CController.php(257): TestsController->runActionWithFilters()
#14 C:\xampp\www\framework\web\CWebApplication.php(320): TestsController->run()
#15 C:\xampp\www\framework\web\CWebApplication.php(120): CWebApplication->runController()
#16 C:\xampp\www\framework\base\CApplication.php(135): CWebApplication->processRequest()
#17 C:\xampp\www\gmat3\index.php(12): CWebApplication->run()
My actual query is :
SELECT q.* FROM questions q LEFT JOIN tests t ON t.id = 1 LEFT JOIN test_section ts ON ts.test_id = t.id LEFT JOIN test_sections tss ON ts.test_section_id = tss.id LEFT JOIN test_section_questions tsq ON tsq.test_id = t.id AND tss.id WHERE q.id = tsq.question_id group by q.id
If I execute this query manually in phpmyAdmin, I am getting the results properly, I don’t know what is happened to my code, where I am doing wrong.
Waiting for your valuable response