Relations in afterFind()

In my afterFind() function i’m calling a relation:


public function afterFind()


      $this->fromName = ($this->from->role==3)?$this->from->company->name:$this->from->username;

      $this->toName = ($this->to->role==3)?$this->to->company->name:$this->to->username;



Works ok, but the problem is that for every row in my model it creates 3 extra sql queries (lazy loading).

If i use:

<?php Message::model()->with(array('from', 'to', '', ''))->findAll(); ?> 

It seems to attach the joins correctly (off course my and needs to be added), but if use ‘with’ in combination with a ActiveDataProvider it still lazy loads, why?

Solved it by adding a defaultScope to my model:

public function defaultScope()


        return array(

            'with' => array('to', 'from')