model->attributes empty ??

i’m building my model (CFormModel ) dynamically

but $model-attributes is empty ?

but if I build the same class attributes manuelly (written in the class),

it all runs fine.

but when using __construct, __get/__set

I spend hours yesterday, turning the problem upside down, found some good post about it , but I still don’t understand ? if someone might have some insight on my error, or is it simpl-Yii ?

Thanks




function __construct($data){

      foreach ($myClassList as $key) {

          $this->$key = $data[$key];

      }

      parent::__construct();

    }

public function __set($key, $value){$this->$key = $value;}

public function __get($value){ return $value;}



Hello.

You can’t redefine __construct fucntion, read API docs. But you can redefine init() method wich runs when model initializing after constructor.

Hmmm I’m not so sure about this

since the construction of the Virtual Class works fine

and all atributes exist, it’s quite strange after creation here’s the result :




var_dump($model->attributes);  //=> string 'attributes' (length=10)

var_dump($model->getAttributes()); //=> empty



but


var_dump($model->getSafeAttributeNames());

returns 

 0 => string 'guests' (length=6)

  1 => string 'respect' (length=7)

  2 => string 'prepared' (length=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

  3 => string 'problems' (length=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

  4 => string 'frequency' (length=9)

  5 => string 'realisation' (length=11)

  6 => string 'finishOntime' (length=12)

  7 => string 'participation' (length=13)

I get the same result (empty attributes), but this is happened when you didn’t defined attributeNames() in your model class.

See below:


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeNames()

    {

        return array(

            'id',

            'username',

            'password',

            //...

        );

    }

after that you will see these attributes, that you listed there

As mentioned above: You’ll need to override the init() method. Then, you will have to work on the _attributes property. That’s an associative array holding all model attributes as $key=>$value pairs.