title: Yii: CHtml image link - how to get to the image in one table through relation in other table?
body: <p>I'm trying to display link to an <em>album/view</em> through image that belongs to that album. I have my model 'Album' and there I have the relation with my 'Photo' model, which represents table where pictures are saved(or to be more accurate-picture names, pictures are saved in the folder 'uploads': </p>
  
  <pre><code>public function relations()
  {
          return array(
                      'photos' =&gt; array(self::HAS_MANY, 'Photo', 'album_id'),
      );
  }
  </code></pre>
  
  <p>Now I'm displaying <em>album/index</em> view, where I parse $dataProvider object which contains all my albums. Now that $dataProvider is parsed to a <em>_view</em> view and there I'm trying to access pictures (actually I just need one picture per album) that belongs to each album displayed. That picture will represent the link to an <em>album/view</em> view where one can see all pictures belonging to that album. I have no ide how to do that. I've tried with: </p>
  
  <pre><code>CHtml::link(
                      CHtml::image(Yii::app()-&gt;request-&gt;baseUrl."/uploads/thumbs/".$data-&gt;photos-&gt;name),
                      array('view', 'id'=&gt;$data-&gt;id));
  </code></pre>
  
  <p>and I got the error: <strong>Trying to get property of non-object</strong></p>
  
  <p>PS: I have no problem with displaying all pictures within one album in <em>album/view</em> view (but these cross controllers, cross table whatever things are giving me hard time). There I have nested <em>photo/index</em> view, and the pictures are displayed like this: </p>
  
  <pre><code>echo CHtml::link(
                      CHtml::image(Yii::app()-&gt;request-&gt;baseUrl."/uploads/thumbs/".$data-&gt;name),
                      Yii::app()-&gt;request-&gt;baseUrl."/uploads/".$data-&gt;name ),
  </code></pre>
  
  <p>(as a link I'm displaying thumbnail of an image, that why i have "/uploads/thumbs/". "name" is column in table which represents filename of an image)</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>@Stu</p>
  
  <p>in my album where I have three photos this: </p>
  
  <pre><code>array (size=3)
    0 =&gt; 
      object(Photo)[84]
        private '_uploads' =&gt; null
        private '_new' (CActiveRecord) =&gt; boolean false
        private '_attributes' (CActiveRecord) =&gt; 
          array (size=6)
            'id' =&gt; string '1' (length=1)
            'album_id' =&gt; string '1' (length=1)
            'name' =&gt; string 'WP_20140907_18_34_12_Pro.jpg' (length=28)
            'caption' =&gt; string 'Balerina' (length=8)
            'date_created' =&gt; string '2014-10-27 19:56:13' (length=19)
            'date_updated' =&gt; null
        private '_related' (CActiveRecord) =&gt; 
          array (size=0)
            empty
        private '_c' (CActiveRecord) =&gt; null
        private '_pk' (CActiveRecord) =&gt; string '1' (length=1)
        private '_alias' (CActiveRecord) =&gt; string 't' (length=1)
        private '_errors' (CModel) =&gt; 
          array (size=0)
            empty
        private '_validators' (CModel) =&gt; null
        private '_scenario' (CModel) =&gt; string 'update' (length=6)
        private '_e' (CComponent) =&gt; null
        private '_m' (CComponent) =&gt; null
    1 =&gt; 
      object(Photo)[85]
        private '_uploads' =&gt; null
        private '_new' (CActiveRecord) =&gt; boolean false
        private '_attributes' (CActiveRecord) =&gt; 
          array (size=6)
            'id' =&gt; string '2' (length=1)
            'album_id' =&gt; string '1' (length=1)
            'name' =&gt; string 'WP_20140907_18_49_33_Pro.jpg' (length=28)
            'caption' =&gt; string '' (length=0)
            'date_created' =&gt; string '2014-10-27 19:56:45' (length=19)
            'date_updated' =&gt; null
        private '_related' (CActiveRecord) =&gt; 
          array (size=0)
            empty
        private '_c' (CActiveRecord) =&gt; null
        private '_pk' (CActiveRecord) =&gt; string '2' (length=1)
        private '_alias' (CActiveRecord) =&gt; string 't' (length=1)
        private '_errors' (CModel) =&gt; 
          array (size=0)
            empty
        private '_validators' (CModel) =&gt; null
        private '_scenario' (CModel) =&gt; string 'update' (length=6)
        private '_e' (CComponent) =&gt; null
        private '_m' (CComponent) =&gt; null
    2 =&gt; 
      object(Photo)[86]
        private '_uploads' =&gt; null
        private '_new' (CActiveRecord) =&gt; boolean false
        private '_attributes' (CActiveRecord) =&gt; 
          array (size=6)
            'id' =&gt; string '3' (length=1)
            'album_id' =&gt; string '1' (length=1)
            'name' =&gt; string 'WP_20140907_18_49_38_Pro.jpg' (length=28)
            'caption' =&gt; string '' (length=0)
            'date_created' =&gt; string '2014-10-27 20:00:41' (length=19)
            'date_updated' =&gt; null
        private '_related' (CActiveRecord) =&gt; 
          array (size=0)
            empty
        private '_c' (CActiveRecord) =&gt; null
        private '_pk' (CActiveRecord) =&gt; string '3' (length=1)
        private '_alias' (CActiveRecord) =&gt; string 't' (length=1)
        private '_errors' (CModel) =&gt; 
          array (size=0)
            empty
        private '_validators' (CModel) =&gt; null
        private '_scenario' (CModel) =&gt; string 'update' (length=6)
        private '_e' (CComponent) =&gt; null
        private '_m' (CComponent) =&gt; null
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>This is really driving me crazy... In the same Album model, I have the other relation with other table "user": </p>
  
  <pre><code>public function relations()
      {
          return array(
              'owner' =&gt; array(self::BELONGS_TO, 'User', 'owner_id'),
              ...
          );
      }
  </code></pre>
  
  <p>And I have no problem accessing attributes in the User model within album view, checked the  var_dump($data->owner): </p>
  
  <pre><code>object(User)[83]
    public 'passSave' =&gt; null
    public 'passRepeat' =&gt; null
    private '_new' (CActiveRecord) =&gt; boolean false
    private '_attributes' (CActiveRecord) =&gt; 
      array (size=7)
        'id' =&gt; string '1' (length=1)
        'email' =&gt; string 'fdhghhg@gmail.com' (length=25)
        'username' =&gt; string 'admin' (length=5)
        'firstname' =&gt; string 'Emina' (length=5)
        'lastname' =&gt; string 'Hasanović' (length=10)
        'pass' =&gt; string 'sa1aY64JOY94w' (length=13)
        'date_created' =&gt; string '2014-10-27 11:20:55' (length=19)
    private '_related' (CActiveRecord) =&gt; 
      array (size=0)
        empty
    private '_c' (CActiveRecord) =&gt; null
    private '_pk' (CActiveRecord) =&gt; string '1' (length=1)
    private '_alias' (CActiveRecord) =&gt; string 't' (length=1)
    private '_errors' (CModel) =&gt; 
      array (size=0)
        empty
    private '_validators' (CModel) =&gt; null
    private '_scenario' (CModel) =&gt; string 'update' (length=6)
    private '_e' (CComponent) =&gt; null
    private '_m' (CComponent) =&gt; null
  </code></pre>
  
  <p>It's the exact same output as with var_dump($data->lastPhoto) (just with it's own attributes), but when I try with $data->lastPhoto->name it's <strong>Trying to get property of non-object</strong>, while $data->owner->username works just well</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Here is my Album model: </p>
  
  <pre><code>&lt;?php
  
  /**
   * This is the model class for table "album".
   *
   * The followings are the available columns in table 'album':
   * @property string $id
   * @property string $name
   * @property string $owner_id
   * @property integer $shareable
   * @property string $date_created
   *
   * The followings are the available model relations:
   * @property User $owner
   * @property Photo[] $photos
   */
  class Album extends CActiveRecord
  {
  
      public function tableName()
      {
          return 'album';
      }
  
  
      public function rules()
      {
  
          return array(
              array('name', 'required'),
              array('shareable', 'numerical', 'integerOnly'=&gt;true),
              array('name', 'length', 'max'=&gt;100),
              array('owner_id', 'length', 'max'=&gt;10),
              array('name', 'safe', 'on'=&gt;'search'),
          );
      }
  
          protected Function beforeSave() {
              if(parent::beforeSave()) {
                  if($this-&gt;isNewRecord) {
                      $this-&gt;owner_id=Yii::app()-&gt;user-&gt;getId();
                  }
                  return true;
              }
              else 
                  return false;            
          }
  
  
      public function relations()
      {           
          return array(
              'owner' =&gt; array(self::BELONGS_TO, 'User', 'owner_id'),
              'photos' =&gt; array(self::HAS_MANY, 'Photo', 'album_id'),
                          'lastPhoto' =&gt; array(self::HAS_ONE, 'Photo', 'album_id', 'order'=&gt;'lastPhoto.id DESC'),
          );
      }
  
          public function scopes()
          {
              return array(
                  'mine'=&gt;array(
                      'order'=&gt;'date_created DESC',
                      'condition'=&gt;'owner_id=:owner',
                      'params'=&gt;array(
                          'owner'=&gt;Yii::app()-&gt;user-&gt;getId(),
                      )
                  )
              );
          }    
  
      public function attributeLabels()
      {
          return array(
              'id' =&gt; 'ID',
              'name' =&gt; 'Name',
              'owner_id' =&gt; 'Owner',
              'shareable' =&gt; 'Shareable',
              'date_created' =&gt; 'Date Created',
          );
      }
  
  
      public function search()
      {           
  
          $criteria=new CDbCriteria;    
  
          $criteria-&gt;compare('name',$this-&gt;name,true);            
          $criteria-&gt;compare('shareable',$this-&gt;shareable);           
  
          $criteria-&gt;scopes='mine';
          return new CActiveDataProvider($this, array(
              'criteria'=&gt;$criteria,
          ));
      }
  
      public static function model($className=__CLASS__)
      {
          return parent::model($className);
      }
  }
  </code></pre>
  
  <p>Controller action is(from AlbumController):</p>
  
  <pre><code>public function actionIndex()
      {
                  $dataProvider=new CActiveDataProvider('Album', array( 
                          'criteria'=&gt;array(
                              'condition'=&gt;'shareable=1',
                              'order'=&gt;'date_created DESC'    
                              )
                          )
                  );
  
          $this-&gt;render('index',array(
              'dataProvider'=&gt;$dataProvider,
          ));
      }
  </code></pre>
  
  <p>And the view files are album/index.php:</p>
  
  <pre><code>&lt;h1&gt;Albums&lt;/h1&gt;
  
  &lt;?php $this-&gt;widget('zii.widgets.CListView', array(
      'dataProvider'=&gt;$dataProvider,
      'itemView'=&gt;'_view',
  )); ?&gt; 
  </code></pre>
  
  <p>_view.php:</p>
  
  <pre><code>&lt;div class="view"&gt;
  
          &lt;h&gt;&lt;?php // echo CHtml::encode($data-&gt;name); ?&gt;&lt;/h1&gt;        
  
          &lt;h1&gt;
             echo CHtml::link(
                      CHtml::image(Yii::app()-&gt;request-&gt;baseUrl."/uploads/thumbs".$data-&gt;lastPhoto-&gt;name),
                      array('view', 'id'=&gt;$data-&gt;id));                   
  
          ?&gt;
          &lt;/h1&gt;
  
          &lt;br /&gt;
          &lt;br /&gt;
  
  
      &lt;b&gt;&lt;?php  echo CHtml::encode($data-&gt;getAttributeLabel('shareable')); ?&gt;:&lt;/b&gt;
      &lt;?php echo CHtml::encode($data-&gt;shareable)? 'Public' : 'No'; ?&gt;
      &lt;br /&gt;
  
      &lt;b&gt;&lt;?php  echo 'Created by'; ?&gt;:&lt;/b&gt;
      &lt;?php echo CHtml::encode($data-&gt;owner-&gt;fullName()); ?&gt;
  
  
      &lt;b&gt;&lt;?php  echo 'on Date'; ?&gt;:&lt;/b&gt;
      &lt;?php echo CHtml::encode($data-&gt;date_created); ?&gt;
      &lt;br /&gt;
  
  &lt;/div&gt;
  </code></pre>