model events

I have the following model:




<?php


/**

 * Active record - CoreLang

 *

 */

class CoreLang extends CActiveRecord

{


    /**

     * Table name property

     *

     * @var string

     */

    public $tableName = 'core_lang';


    /**

     * Class constrcutor instance

     *

     * @param object $className

     * @return object

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * Table name method

     *

     * @return string

     */

    public function tableName()

    {

        return $this->tableName;

    }


    /**

     * Relations between tables

     *

     * @return unknown

     */

    public function relations()

    {

        return array(

            'lang_id' => array(self::HAS_MANY, 'CoreLangWords', 'lang_id'),

        );

    }

    

    public function onAfterFind($event)

    {

        return $event->sender->getAttributes();

    }

    

    /**

     * Get core langs

     *

     * @param unknown_type $limit

     * @return unknown

     */

    public function all($limit=5)

    {

        $this->getDbCriteria()->mergeWith(array('limit' => $limit));

        return $this;

    }

}



I am calling it like so:


$lang = CoreLang::model()->all()->findAll();

print_r($lang);

It returns all the AR DB schema, relations etc… if you notice i added an event onAfterFind which returns an array of just the attributes returned.

If i do a print_r() for the $event->sender->getAttributes(); i get the following:


Array

(

    [lang_id] => 1

    [lang_locale] => en

    [lang_short] => en_US

    [lang_title] => English (USA)

    [lang_currency_name] => USD

    [lang_currency_symbol] => $

    [lang_decimal] => .

    [lang_comma] => ,

    [lang_default] => 1

    [lang_isrtl] => 0

)

Which is what i wanted, The thing is that i called the model by


$lang = CoreLang::model()->all()->findAll();

print_r($lang);

and the print_r above returns all the AR DB schema, relations and everything. How can i bypass that to return the array i built using the event onAfterFind ?

Thanks.

Bump.

Hi,

maybe it’s too late down here, but it can’t get it why you think that the return value of onAfterFind would replace the results of findAll?!

Greets

Well, First of all it does work. If i put a var_dump($event->sender->getAttributes()); in the ofafterfind method it does print what the findall returned. But i would like it to change the actual return object.

Of course you can access the sender object in OnAfterFind. It is already an instantiated record and you may also manipulate it as you like.

But to completely replace the returned object imo it would make more sense to override the populateRecords function, doesn’t it?

Oh, That’s what i was looking for!!

I added


public function populateRecords($data, $callAfterFind=true)

    {

        return $data;

    }

and it worked it only returned the rows and nothing else.

Thanks.