Yii2: setScenario method chaining in yii\base\Model

Created on 17 Nov 2016  路  6Comments  路  Source: yiisoft/yii2

It would e very convenient to use
if ($model->setScenario('edit')->load(Yii::$app->request->post()) && $model->validate()) {

At present the method does not return anything, so no breaking changes..
New method as:
public function setScenario($value) { $this->_scenario = $value; return $this; }

I can create PR if this suggestion is accepted..

under discussion enhancement

Most helpful comment

Does not make sense for me.
Model has not been designed for chaning syntax. Such methods like load() or validate() can not return its reference,
Chaining make sense in case 3 or more methods can appear in the sequence, creating some sentence, like yii\db\Query does. Chaning of 2 methods makes program hard to read as second method can sweep the glance.

All 6 comments

Could be implemented. Let's wait for other opinions before submitting a new PR

It's OK to implement.

it would be nice to have some consistency on this matter. Why would we enable return $this for this setter and not for other setters. Either we choose to return $this for all setters, or not. This is a question to be answered in general.

Does not make sense for me.
Model has not been designed for chaning syntax. Such methods like load() or validate() can not return its reference,
Chaining make sense in case 3 or more methods can appear in the sequence, creating some sentence, like yii\db\Query does. Chaning of 2 methods makes program hard to read as second method can sweep the glance.

Both @dynasource and @klimov-paul are right about inconsistencies and that pretty much out weights my and @SilverFire no-objections. Won't be implemented in this case.

Was this page helpful?
0 / 5 - 0 ratings