Yii Framework Forum: Hierarchy scenario in Active Record - Yii Framework Forum

Jump to content

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

Hierarchy scenario in Active Record

#1 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 17 September 2011 - 09:48 PM

To me, scenario is useful. For example when writing different scenarios at the validation rules() or events(beforeSave, afterSave) in Active Record. But when the Active Record become complex the number of scenarios become more and more. When there are too many scenarios, I think it would be better if we can just group some of scenarios into one name, and we only need to define that name

for example like this code:
<?php

public function scenarios()
{
            return array(
                 'generalScenarios'=>array('registration','changeProfile'),
            );
}

public function rules() {
		return array(
			array('name, username', 'required', 'on'=>'generalScenarios'),
                );
}

public function afterSave()
{
           if ($this->checkScenario('generalScenarios')){
                //do something
           }
}

“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#2 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 22 September 2011 - 02:51 AM

Good idea, but it's more likely an extra feature, not something that is implemented in the first releases, because it isn't really a design change.
0

#3 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 199
  • Joined: 09-March 09
  • Location:*.php

Posted 23 October 2011 - 12:20 PM

A great idea to finally get confused :)

Much more clearly:
public function rules() {
                return array(
                        array('name, username', 'required', 'on'=>'registration, changeProfile'),
                );
}

and
public function afterSave()
{
           if($this->scenario==='registration' || $this->scenario==='changeProfile')
           {
                //do something
           }
}


Because when then need:
public function afterSave()
{
           if($this->scenario==='registration')
           {
                //do some thing
           }

           if($this->scenario==='changeProfile')
           {
                //do other thing
           }
}

You will do degroup, and I just change my code.

But if you want even so grouping than you can do it right now:
public function rules() {
                $generalScenarios=array('registration', 'changeProfile');

                return array(
                        array('name, username', 'required', 'on'=>$generalScenarios),
                );
}


Not more. A new method scenarios() for scenario grouping... hm...
No good, no bad, only consequence.
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