[SOLVED]Yii join results

I am joining two tables (photos and photos_adminvotes) but the problem is when i am using a var_dump on the model i am not getting the photos_adminvotes attributes like for example $model->vote?

My table structure.




photos

------

id

user_id 

catid

title

equipment

description

original

size

created

status


photos_adminvotes

-----------------

id

photo_id

user_id

vote



Code in PhotosAdminController




$criteria = new CDbCriteria;

$criteria->join = 'LEFT JOIN photos_adminvotes ON photos_adminvotes.photo_id = '.$id;

$criteria->condition = 't.id = photos_adminvotes.photo_id';


$model=PhotosAdmin::model()->find($criteria);


var_dump($model);



var_dump result




object(PhotosAdmin)[107]

  private '_md' (CActiveRecord) => 

    object(CActiveRecordMetaData)[93]

      public 'tableSchema' => 

        object(CMysqlTableSchema)[92]

          public 'schemaName' => null

          public 'name' => string 'photos' (length=6)

          public 'rawName' => string '`photos`' (length=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

          public 'primaryKey' => string 'id' (length=2)

          public 'sequenceName' => string '' (length=0)

          public 'foreignKeys' => 

            array

              ...

          public 'columns' => 

            array

              ...

          private '_e' (CComponent) => null

          private '_m' (CComponent) => null

      public 'columns' => 

        array

          'id' => 

            object(CMysqlColumnSchema)[94]

              ...

          'user_id' => 

            object(CMysqlColumnSchema)[95]

              ...

          'catid' => 

            object(CMysqlColumnSchema)[96]

              ...

          'title' => 

            object(CMysqlColumnSchema)[97]

              ...

          'equipment' => 

            object(CMysqlColumnSchema)[98]

              ...

          'description' => 

            object(CMysqlColumnSchema)[99]

              ...

          'original' => 

            object(CMysqlColumnSchema)[100]

              ...

          'size' => 

            object(CMysqlColumnSchema)[101]

              ...

          'created' => 

            object(CMysqlColumnSchema)[102]

              ...

          'status' => 

            object(CMysqlColumnSchema)[103]

              ...

      public 'relations' => 

        array

          empty

      public 'attributeDefaults' => 

        array

          empty

      private '_model' => 

        object(PhotosAdmin)[91]

          private '_md' (CActiveRecord) => 

            &object(CActiveRecordMetaData)[93]

          private '_new' (CActiveRecord) => boolean false

          private '_attributes' (CActiveRecord) => 

            array

              ...

          private '_related' (CActiveRecord) => 

            array

              ...

          private '_c' (CActiveRecord) => null

          private '_pk' (CActiveRecord) => null

          private '_alias' (CActiveRecord) => string 't' (length=1)

          private '_errors' (CModel) => 

            array

              ...

          private '_validators' (CModel) => null

          private '_scenario' (CModel) => string '' (length=0)

          private '_e' (CComponent) => null

          private '_m' (CComponent) => null

  private '_new' (CActiveRecord) => boolean false

  private '_attributes' (CActiveRecord) => 

    array

      'id' => string '1' (length=1)

      'user_id' => string '1' (length=1)

      'catid' => string '1' (length=1)

      'title' => string 'Croatia' (length=7)

      'equipment' => string '' (length=0)

      'description' => string '' (length=0)

      'original' => string '1.jpg' (length=5)

      'size' => string '251754' (length=6)

      'created' => string '2011-06-01 07:52:50' (length=19)

      'status' => string '1' (length=1)

  private '_related' (CActiveRecord) => 

    array

      empty

  private '_c' (CActiveRecord) => null

  private '_pk' (CActiveRecord) => string '1' (length=1)

  private '_alias' (CActiveRecord) => string 't' (length=1)

  private '_errors' (CModel) => 

    array

      empty

  private '_validators' (CModel) => null

  private '_scenario' (CModel) => string 'update' (length=6)

  private '_e' (CComponent) => null

  private '_m' (CComponent) => null






$criteria->join = 'LEFT JOIN...';



The code above will create a JOIN in the generated SQL but won’t fetch any data.

You should define the relation between photos and photos_adminvotes in the model classes and use:




PhotosAdmin::model()->with(array('..relations'))



or:




$criteria->with = array('...relations');



Relational Active Record guide

Ok i did the following in my PhotosAdmin but then the relation is between both the id’s and not between the photos id and photos_adminvotes photo_id?




/**

 * @return array relational rules.

 */

public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'photo_id'=>array(self::BELONGS_TO, 'PhotosAdminvotes', 'id'), but then the relation is 

	);

}



I think the correct relation is: PhotosAdmin HAS_MANY PhotosAdminvotes.

Define the foreign key name in the relation instead of the primary key of related table:




'votes' => array(self::HAS_MANY, 'PhotosAdminvotes', 'photo_id'),



Yes that did it, thanks for helping me out with this one.