Yii Framework Forum: Default Db Value Override - Yii Framework Forum

Jump to content

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

Default Db Value Override Rate Topic: -----

#1 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 427
  • Joined: 18-November 10
  • Location:UK

Posted 10 December 2012 - 04:03 PM

When creating new records with Yii, Yii is putting the DB default values for those fields into the form fields.

How can I override this?
0

#2 User is offline   Anthony Gough 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-October 12

Posted 10 December 2012 - 05:12 PM

One method would be to use the afterFind() method of CActiverecord to replace the default database values
0

#3 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 427
  • Joined: 18-November 10
  • Location:UK

Posted 11 December 2012 - 07:24 AM

View PostAnthony Gough, on 10 December 2012 - 05:12 PM, said:

One method would be to use the afterFind() method of CActiverecord to replace the default database values


Hi,

Yes I tried that but it does not replace them, at least it does not on create anyway.

James.
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,595
  • Joined: 04-March 10
  • Location:UK

Posted 11 December 2012 - 08:24 AM

Try overriding afterConstruct().

The constructor is where the defaults are set:
public function __construct($scenario='insert') 
{ 
    if($scenario===null) // internally used by populateRecord() and model() 
        return; 

    $this->setScenario($scenario); 
    $this->setIsNewRecord(true); 
    $this->_attributes=$this->getMetaData()->attributeDefaults;

    $this->init(); 

    $this->attachBehaviors($this->behaviors()); 
    $this->afterConstruct(); 
}

0

#5 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,323
  • Joined: 21-February 11
  • Location:Greece

Posted 11 December 2012 - 09:03 AM

just insert in your model class

public $field1 = 'value1';
public $field2 = 'value2';
public $field3 = 'value3';

It works in my project :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
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