Add Attributes After Finall Function In Model

Hi Friends,

I am calling the findall method and i am getting 4 fields.I now want to add one more field named $owned.

SO that means after i get the record form the table, the resultant datarecord should contain the owned field.

Also the $owned field is dynamic based on whether user is the owner of the group. I tried using afterFind. But it is also not working. Surprisingly it is adding attributed owned to the object but not to attributes.

below is the code

/**

  • The followings are the available columns in table ‘group’:

  • @property integer $id

  • @property string $name

  • @property string $created_at

  • @property string $updated_at

*/

class Group extends CActiveRecord

{

//adding owned property for groups.true if user is owner

public $owned;

protected function afterFind()

{

parent::afterFind();

//if user is owner of group its true

$this->owned = true;

}

Thanks to all

Smith




protected function afterFind()

{

//if user is owner of group its true

$this->owned = true;

return parent::afterFind ();

}




Thanks Jimlam,

I tried this but still the property is not showing.

when i did print_r($model) in controler, i find that the property $owned adds to the top of model object and not to attributes.

like this

Array

(

[0] => group Object


    (


        [owned] => 1


        [_md:CActiveRecord:private] => CActiveRecordMetaData Object


            (


                [tableSchema] => CMysqlTableSchema Object


                    (


                        [schemaName] => 


                        [name] => group


                        [rawName] => `group`


                        [primaryKey] => id


                        [sequenceName] => 


                        [foreignKeys] => Array


                            (


                            )





                        [columns] => Array


                            ( ....................AND SO ON

WHereas in attributes for the model it shows

[_attributes:CActiveRecord:private] => Array

            (


                [id] => 1


                [name] => tab1


                [created_at] => 0000-00-00 00:00:00


                [updated_at] => 0000-00-00 00:00:00


          


            )

As you can see that owned property is not showing up here.!

Thanks

Smith

Try initialising the value to false




public $owned = false;

protected function afterFind()

{

//if user is owner of group its true

$this->owned = true;

return parent::afterFind ();

}




Tried this too…Did not work:( …Is it a bug?

and i am not able to understand why the property $owned gets added to the top level of object tree…dosnt go to attributes at all…Upon doing CJSON::encode($model) i just dont see the property…

Many thanks for the answer…

I have given up guys…Just not happening.

What does it render in your view files? For example, if you have the default _view.php, what does $data->owned render?

Thanks Jimlam,

I dont have a view.I have a MVC front end which is javascript backbone. i am just testing it from the browser by loading the controller url.

I am getting output of url as json using CJSON::encode() which is not showing me $owned property.

Many Thanks

Smith

Hi Friends, Finally i found out. It is the problem with the CJSON::encode problem. this method only encodes the properties of the model. It doesn’t encode any relational or dynamic property. SO i wrote my own function and it works.!

Thanks to all

Smith

I know this is an old Thread but I have “the same” problem. Is there any way to get the new field into the attributes?

ok, my mistake. I can access the data. But it is still confusing that they are not to be found under attributes but toplevel.