MYSQL RELATIONAL QUERY

Here is my database structure (simplified):

TABLE properties

id (PK, INT, AUTO INC)

prop_vebraid (INT)

TABLE images

id (PK, INT, AUTO INC)

image_url (VARCHAR)

prop_vebraid (INT)

file_index (INT)

I am using MySQL database with MyISAM format, so I don’t have any foreign keys in the database.

I’m currently outputting all fields from the properties table in my Controller $models=Property::model()->findAll($criteria);

I now need to extend this so that it returns the matching image_url from the images table. The matching field in both tables is prop_vebraid. As one property can have many images, it must also check that file_index in the images table is 1.

Does anyone have any idea how I can do this, as I’m having some difficulty doing it.

Can anyone advise?

yii cannot support master model without primary key to relate HAS MANY to another model in disguise~ :(

I don’t get it…

I’m sure this can be done in normal PHP so surely Yii can do it too…

OK I figured it out myself. I did this in my Property Model:


public function mainImage()

{

	return Image::model()->find('prop_vebraid=:prop_vebraid AND file_index=1', array(':prop_vebraid'=>$this->prop_vebraid));

}

And in my view I just access it like this:


<?php echo CHtml::image($model->mainImage()->file_url, 'Main Image'); ?>