Tables With Same Column Name In Query Builder


$images = Yii::app()->db->createCommand()->select('ul.*, imgs.*')

				 ->from('unitlistings ul, images imgs')

				 ->where("ul.refno = imgs.refno AND ul.refno = '$refno'")

				 ->queryall();

In view,


foreach($images as $img){

                        .................

			$srcorig = $img['imageURL'];


.............

The tables unitlistings and images both have a column imageURL. $img[‘imageURL’] refers to the value in the imageURL column of images table. How can I get the imageURL value from the unitlistings table ?

You can either specify the individual columns that you want instead of finding all by wildcard, or you can duplicate one or both columns with different aliases. The second option would look something like this:




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

    ->select('ul.*, imgs.*, ul.imageURL AS ulImageURL')

    ->from('unitlistings ul, images imgs')

    ->where("ul.refno = imgs.refno AND ul.refno = '$refno'")

    ->queryall();


foreach($images as $img){

    .................

    $srcorig = $img['ulImageURL'];


.............



That worked. Thanks