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
Page 1 of 1
Add Attributes After Finall Function In Model How to add attribute after findall() database call in model
#2
Posted 17 December 2012 - 10:16 AM
protected function afterFind()
{
//if user is owner of group its true
$this->owned = true;
return parent::afterFind ();
}
#3
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
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
#4
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 ();
}
#5
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..
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..
#7
Posted 17 December 2012 - 01:21 PM
smith19, 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..
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?
#8
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
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
#9
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
Thanks to all
Smith
Share this topic:
Page 1 of 1

Help













