I have a small user console that allows the user to only see their account.
When invoking the url to the console without being logged in (user_id is not set)
"http://localhost/q1partner/index.php?r=demandpartner/console"
I was getting an error like the following:
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1. The SQL statement executed was: [b]SELECT * FROM `demandpartner` `t` WHERE acct_id= LIMIT 1 [/b]
My Controller:
<?php
class DemandpartnerController extends Controller {
public function actionConsole() {
$model= $this->loadModel();
$this->render('console', array(
'model' => $model,
));
}
public function loadModel() {
[b]if isset(myAccount()) {[/b] /* I added this to catch error */
$model= Demandpartner::model()->myAccount()->find();
if($model===null)
throw new CHttpException(404,Yii::t('app', 'The requested page does not exist.'));
return $model;
}
}
}
My Model has a Scope as follows:
public function scopes()
{
return array(
'myAccount'=>array('condition'=>"acct_id=".Yii::app()->user->id,),
);
}
Is there a more graceful way to deal with this?
Would not $model= Demandpartner::model()->myAccount()->find(); be null if the scope is not set?
Should I test for myAccount or user.id to be set ?
Thanks!