Yii Framework Forum: How to add more property into model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to add more property into model Rate Topic: ***** 1 Votes

#1 User is offline   logos010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 12-November 10
  • Location:VietNam

Posted 20 January 2011 - 05:16 AM

Hello guys,
When I use gii tool to general a model, it default general all fields to model's property, right?
So, when I wanna add a new field into my table, how can I add this property into model.
I'd tried:
class myModel extends CActiveRecord{
     public $newProperty;

     public function _get(){
         return $this->newProperty;
     }

     public function _set($newProperty){
         $this->newProperty = $newProperty;
     }

}

Is there somethins wrong with my codes, when I try to display the new property in gridview:
....
array(
  'header' => 'New Property',
  'type' => 'raw',
  'value' => $date->newProperty
)

Nothing display with my code :D
any suggestion :D?
Thanks
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,543
  • Joined: 12-October 09
  • Location:Croatia

Posted 20 January 2011 - 05:22 AM

use just public $newProperty... without the _get() and _set()

But that is just for "additional" attributes ie those that are not part of the database...

If you just add a new field to the table... you don't need to declare a variable... just set some validation rules... and use the new field as $model-><fieldname>
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   logos010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 12-November 10
  • Location:VietNam

Posted 21 January 2011 - 10:38 AM

View Postmdomba, on 20 January 2011 - 05:22 AM, said:

use just public $newProperty... without the _get() and _set()

But that is just for "additional" attributes ie those that are not part of the database...

If you just add a new field to the table... you don't need to declare a variable... just set some validation rules... and use the new field as $model-><fieldname>


Hi, thanks for reply Mdomba.
I'd tried you way, if I add $newProperty without _get() and _set(), it displays nothing!
Well, if I set validation rules, it bring me error: "Property model_name.newProperty is not defined."
I'd added into rules() and attributeLabels(), but it seem not work :D
0

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,462
  • Joined: 04-October 10

Posted 21 January 2011 - 10:51 AM


class myModel extends CActiveRecord{
     private $_newProperty;

     public function getNewProperty(){
         return $this->_newProperty;
     }

     public function setNewProperty($newProperty){
         $this->_newProperty = $newProperty;
     }

}

// to use it
$model->newProperty = 'myvalue';

echo $model->newProperty;


Check the magic __set and __get methods of CComponent.
0

#5 User is offline   logos010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 12-November 10
  • Location:VietNam

Posted 21 January 2011 - 11:01 AM

Thank you so much for reply! :lol:
I'd fixed this problem. As Mdomba said, I just need to add some validation rules for the $newProperty -> it work :D
And I found the same topic here.
Thanks again Mdomba, Antonio Ramirez
@Antonio Ramirez: I'll try your suggest, may be that's another way to this problem, thanks again. :D
Cheers
0

#6 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,462
  • Joined: 04-October 10

Posted 21 January 2011 - 11:07 AM

View Postlogos010, on 21 January 2011 - 11:01 AM, said:

Thank you so much for reply! :lol:
I'd fixed this problem. As Mdomba said, I just need to add some validation rules for the $newProperty -> it work :D
And I found the same topic here.
Thanks again Mdomba, Antonio Ramirez
@Antonio Ramirez: I'll try your suggest, may be that's another way to this problem, thanks again. :D
Cheers


My approach is more of an extension, widget concept... mdomba suggestion is more model realistic... congrats man
0

#7 User is offline   mrun 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-August 14

Posted 13 October 2015 - 03:42 AM

Hey guys,
I've been fighting with the same issue for the past couple of hours and finally found the solution that worked for me.
So I just wanted to share that you would need to flush your cache
Yii::app()->cache->flush()
if you have enabled the caching of the schema via the 'schemaCachingDuration' in your database connection configuration.

Cheers
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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