Formulate Relational Query...

Hey guys…

I’m trying to put the following SQL Statement in a DBCritera Objekt:

SELECT * FROM tb_image, tb_model WHERE tb_image.idModel=tb_model.idModel AND tb_model.UFS=0 

Can anybody help?


I assume that you want to get all model instances of the ActiveRecord related to the table tb_model with their corresponding image where the attribute UFS=0. So try this:

$criteria=new CDbCriteria;




This loads all models that have UFS=0 and their images into the variable $models. Note that the Model AR class has to have a relation called "image"

If you really just want to use a simple SQL query and need the SQL results as an array and not as AR instances you could use the query builder and not the CDBCriteria:

$modelsandimages = Yii::app()->db->createCommand()



    ->where('tb_model.UFS=:UFS', array(':UFS'=>0))