Yii Framework Forum: override init CActiveRecord - Yii Framework Forum

Jump to content

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

override init CActiveRecord Rate Topic: -----

#1 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 12 March 2010 - 09:06 AM

Hi

I have a problem redefining the init method in one of my models extending CActiveRecord class

The init method does not execute when I do a find (but it executes when I do a new)

The problem: public function __construct
When I call a find method, this condition is true and return before call the init()
if($scenario===null) // internally used by populateRecord() and model()
                        return;

Why?

I'm redefining the scenario in the init
// init in my model
public function init()
    {
      
      if($condition))
          $this->scenario='scenario1';
      else
          $this->scenario='scenario2';

      return parent::init();
    }


What can I do?
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 12 March 2010 - 05:35 PM

I've run into the same problem and created a change request on the google code Yii project, but Qiang has denied my request.

I've simply overridden the __constructor in my class like this:

class UserModel extends CActiveRecord
{
	...

	public function __construct($scenario='insert')
	{
		parent::__construct($scenario);

		// Fix problem for creation by instantiate()
		if($scenario===null){ // internally used by populateRecord() and model()
			$this->init();

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

	...
}


As you can tell from this code fragment the attachBehaviors and afterConstruct are also not called by the default constructor when calling the model() method.
0

#3 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 12 March 2010 - 05:51 PM

Hi, thanks for reply

View PostOnman, on 12 March 2010 - 05:35 PM, said:

, but Qiang has denied my request.


why?
which was the answer or the reason?
What solution he proposes?


Remember the issue ?

greetings
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#4 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 12 March 2010 - 06:03 PM

Here's the answer he added when closing my request:

Quote

Updates:
Status: Invalid

Comment #1 on issue 949 by qiang.xue: AR: __construct(null) does not attach behaviors
http://code.google.c...s/detail?id=949

This is by design. You should not pass it a null.

--
You received this message because you are listed in the owner or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.c...osting/settings


I wasn't satisfied with this answer but have been too busy to work on my project, therefore I never reacted on this mail (maybe I'll do so, later, sometime).
0

#5 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 12 March 2010 - 06:23 PM

View PostOnman, on 12 March 2010 - 06:03 PM, said:

Here's the answer he added when closing my request:


I wasn't satisfied with this answer but have been too busy to work on my project, therefore I never reacted on this mail (maybe I'll do so, later, sometime).


maybe the solution (for me)
is use afterFind and not init for set the scenario
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#6 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 15 March 2010 - 05:58 AM

Solved for the master :rolleyes: in 1.1.1

- Chg #949: The init() method will be invoked after an AR instance is created by the find methods (Qiang)
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
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