I need to search for the primary key value from a database table by using a value from another column and pass the primary key in the loadModel argument. The code I used in the loadModel method is shown below,
$sql = "SELECT itemNo FROM inv_item WHERE medcineId =$id";
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$orderList=$command->queryAll();
$model=Item::model()->$orderList[0];
if($model===null)
//throw new CHttpException(404,'The requested page does not exist.');
return $model;
The problem is, that when I run this page it displays undefined offset error message
it does not make much sense unless there is attribute in model with name equal to "itemNo" value… if this is a foreign key you should rather use:
$model=Item::model()->findByPk($orderList[0]);
also look at this part:
if($model===null)
//throw new CHttpException(404,'The requested page does not exist.');
return $model;
it means that $model will be returned only when it is null, because "throw" line is commented. Otherwise nothing is returned.
This is why I am always putting {} braces after if/for/while etc, and thing I do not like in Yii code…
Anyway - If above does not solve your problem, check your logs (in protected/runtime directory) and see the callstack where exactly is the error thrown and paste it here.