Init() In Cactiverecord, Variable Null

Hello

I have an object inside my model (CActiveRecord), I have to initialize this object with a property from the database. To do that I use the init() method but the property is null

here the code




class CompanyFidelityCard extends CActiveRecord

{

    

        public $Privacy;

        

        public function init()

        {

            $this->Privacy=new PrivacyCode($this->privacy_code);

            parent::init();

        }

.........       



$this->privacy_code is null coz if I change it for this

$this->Privacy=new PrivacyCode(‘1111111111111111111111111111111111111111’);

works

I have tried as well with afterConstruct() method by the result is the same.

any help? thanks in advance

PD; sorry if I have done a mistake is my first post, I have read the guidline and search about this here…

PPD;sorry for my english too

Hi Adria, welcome to the forum.

Well, it seems to me that you are trying to use CActiveRecord in a very strange way. CActiveRecord is not designed to be used like that. You should not call a heavy method from init().

For instance, if you call CompanyFidelityCard::model()->findAll(), then init() will be called as many times as there are rows in company_fidelity_card table.

You should consider making use of relational active record functionality instead.

Hi Adria

I agree with softark

First of all, is attribute privacy_code initialized ?

If initializes from database then use the afterFind

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#afterFind-detail