Problem with findBySql function

I try to make sql query


$picture = Picture::model()->findBySql('SELECT * FROM tbPictures

                LEFT JOIN tbAlbums ON tbPictures.idtbAlbums = tbAlbums.idtbAlbums

                LEFT JOIN tbPersons ON tbAlbums.idtbPerson = tbPersons.idtbPerson

                WHERE tbPersons.idtbPerson='.$idperson);

the query work good, but I need to get idtbPerson from it.

I write echo $picture->idtbPerson - but i Get an error: "Property "Picture.tbPerson" is not defined"

((( How can I get it

Try out after adding $idtbPerson variable to your model class.

It is not possible to get related table fields this way unfortunately. The model this way can return only return data for its own fields.

To get the related data you must use the model in the following way:

$picture = Picture::model()->with(‘tbAlbums_relation’)->find(“where clause”)…

You can not directly use echo $picture->idtbPerson . you need foreach loop for that

foreach($picture as $data)

{

 echo $data['idtbPerson'];

}

otherwise

Use FindAll() instead of findBySql() and after this query…you can retrieve data using foreach loop…

foreach($picture as $data)

{

 echo $data['idtbPerson'];

}

Hi,

you can use CDBCriteria…

please see the link…

http://www.yiiframework.com/forum/index.php/topic/45013-convert-sql-to-yii-query/page__p__213035__fromsearch__1#entry213035