What I would like to do is find 2 variables from table ULD (idULD, idUser, idLocation, idActivity). I’d like to get idLocation and idActivity and put it into next query to get the data from table OS. The problem is that in dropdown list I want to fill, all I get is: “Inventarna - Naziv (NRedni)” instead of all values that are in the table.
Basically what I want to do is in MySQL sentence:
SELECT * FROM OsnovnoSredstvo WHERE idLocation LIKE (SELECT idLocation FROM ULD WHERE idUser=’.Yii::app()->user->id.’)
AND idActivity LIKE (SELECT idActivity FROM ULD WHERE idUser=’.Yii::app()->user->id.’);
Here is my code:
$OsnovnoSredstvo = OsnovnoSredstvo::model()->findByAttributes(array(),
$condition = 'idActivity LIKE :param1 AND idLocation LIKE :param2',
$params = array(
':param1' => $idVars->idActivity,
':param2' => $idVars->idLocation,
$list = ExtHtml::listData($OsnovnoSredstvo , 'Inventarna', array('Inventarna',' - ', 'Naziv', '(','NRedni',')'));
And according to the reference of CDbCriteria::addSearchCondition, this should create the same SQL as OPTION No.1.
CDbCriteria::addSearchCondition will make ready binding barameters for you. So you don’t have to set $params manually when you use it to construct the criteria. But when you want to supply the condition with a plain text, you have to set the binding parameters explicitly.
Now, I think your OPTION No.1 has no error and is working as expected. I mean that the empty array is the right result. So you may want to check the return value of ULD::model()->find.
$idVars = ULD::model()->find('idUser=:User',array(':User'=>Yii::app()->user->id));
// check if $idVars is what you expect here
Thank you all, guys for a lot of help! The last thing softark wrote did the trick and as I expected I do not even need to put ‘%’ into the code, because I already read % sign from database. Thank you all again for quick replys and big help!