In my user registration controller, I’m doing something along the lines of this:
$user = new User('signup');
if(isset($_POST['User']))
{
$user->attributes = $_POST['User'];
if ($user->save())
{
return $this->redirect('site/login',false);
}
}
$this->render('index', array('user' => $user));
No problem with that, per se.
However, in User::beforeSave(), I have some conditional logic that depends on the current scenario.
Specifically, I have some code that should only execute when the scenario is NOT ‘signup’. In other words, my beforeSave() method contains something along the lines of this:
if ($this->getScenario()!=='signup')
{
// some code that does not apply during sign-up...
}
My problem is that this code ALWAYS executes. Why? Because of the way that the scenario is changed internally by CActiveRecord itself while it’s saving. CActiveRecord::populateRecord() and CActiveRecord::insert(), for example, both temporarily change the scenario to ‘update’.
That seems wrong to me - ‘update’ is not a scenario, it’s something else. Something that describes an internal condition to CActiveRecord.
I’m guessing the reason that it’s exposed this way, is so that you can write validation rules that apply only while active record is updating.
While that may be relevant, I may still need to know the actual scenario - the condition that I put my model into… you can’t just hide or erase the current scenario, it is very likely important to the developer - in my case, it’s preventing me entirely from doing what I need to do. I can’t think of any way to work around this, other than hacking CActiveRecord.
This to me indicates a design flaw.
It seems to me that the current internal operation/state of CActiveRecord can not be considered a “scenario” as such, since it does not describe the condition of my model. It’s more like a “state” or perhaps “operation”, and should not be mixed in with the scenario in which I’m using my model.
What’s worse is that there is no backwards compatible way to fix this issue, as far as I can figure…