Yii Framework Forum: Add Attributes After Finall Function In Model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Add Attributes After Finall Function In Model How to add attribute after findall() database call in model Rate Topic: -----

#1 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 17 December 2012 - 09:45 AM

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
0

#2 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 17 December 2012 - 10:16 AM

protected function afterFind()
{
//if user is owner of group its true
$this->owned = true;
return parent::afterFind ();
}


0

#3 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 17 December 2012 - 10:37 AM

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
0

#4 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 17 December 2012 - 11:37 AM

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 ();
}


0

#5 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 17 December 2012 - 12:26 PM

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..
0

#6 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 17 December 2012 - 01:16 PM

I have given up guys..Just not happening.
0

#7 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 17 December 2012 - 01:21 PM

View Postsmith19, on 17 December 2012 - 12:26 PM, said:

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..


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

#8 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 17 December 2012 - 01:39 PM

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
0

#9 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 20 December 2012 - 02:42 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users