Relations in afterFind()

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




<?php

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', 'to.company', 'from.company'))->findAll(); ?> 

It seems to attach the joins correctly (off course my from.company and to.company 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')

        );

}