Virtual properties not populated on afterFind()

Hello there !

I have a strange behaviour with [font="Lucida Console"]afterFind()[/font]/[font="Lucida Console"]findAll()[/font] combination.

There is a model with ghost properties which are filled at [font="Lucida Console"]afterFind()[/font]:


    

protected function afterFind()

{

    	// echo "::afterFind() id <{$this->id}> number_start <{$this->number_start}> number_limit <{$this->number_limit}>";


        $this->number_usage = array();

        $this->number_count = 1 + $this->number_limit - $this->number_start;


        for( $number = $this->number_start; $number <= $this->number_limit; $number++ )

        {

            $this->number_usage[ $number ] = (int) substr( $this->availability, $number - $this->number_start, 1 );

            $this->number_count-= $this->number_usage[ $number ];

        }


        parent::afterFind();

}



For example, ghost property [font=“Lucida Console”]number_usage[/font] did came filled at gridview, but it doesn’t come populated after a [font=“Lucida Console”]findAll()[/font].

I place the debugging echo at [font=“Lucida Console”]afterFind()[/font] discovering that if I issue a [font=“Lucida Console”]model()::findAll()[/font], [font=“Lucida Console”]$this->number_start[/font] neither [font=“Lucida Console”]$this->number_limit[/font] (which aren’t ghost properties) don’t come with values.

So please enlight me…

Thank you,

cbi

First of all check if these values are assigned in your rules to a scenario or at least safe.

Hello bettor,

and thanks for you answer.

This is rules() method:


	

return array(

	array('number_start, number_limit, name', 'required'),

	array('fund_account_id', 'length', 'max'=>10),

        array('name', 'length', 'max'=>127),

	array('number_start, number_limit, number_current', 'length', 'max'=>20),

	array('date, notes, last_issue', 'safe'),

	// The following rule is used by search().	

	array('id, fund_account_id, date, number_start, number_limit, number_current, number_usage, number_range, notes', 'safe', 'on'=>'search'),

        array('updated', 'default', 'value' => new CDbExpression('Now()'), 'setOnEmpty' => false, 'on' => 'update'),

	array('created, updated', 'default', 'value' => new CDbExpression('Now()'), 'setOnEmpty' => False, 'on' => 'insert' )

);



Something is missing??

cbi

I found a workaround:


$checkBooks = CheckBook::model()->findAll( $criteria );


if( count( $checkBooks ) )


       	foreach( $checkBooks as $checkBook )

       	{

        	// Here $checkBook model has only <id> property populated

    		$checkBook = CheckBook::model()->findByPk( $checkBook->id );


        	// Now $checkBook model has all virtual properties populated

       	}



Now, I like to know if it is by design that [font=“Courier New”]:afterFind()[/font] isn’t called after [font=“Courier New”]:findAll()[/font].

Thank you,

cbi

According to (my understanding of) the source code, afterFind() is called after a findAll() call.