@@ -186,6 +186,224 @@ Such values are not allowed:
186186
187187If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be considered ignoring `enum`.
188188
189+ # ## `x-scenarios`
190+
191+ Automatically generated scenarios from the model 'x-scenarios'.
192+ Each scenario is assigned attributes as in the 'default' scenario.
193+ The advantage is that the scenario can be used immediately.
194+ This can be overridden in the child model if needed.
195+
196+ The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
197+ are automatically included in the scenarios() function for the model.
198+
199+ There are three ways to define the scenarios `description` :
200+ - use scenario description default settings `public $scenarioDefaultDescription = "Scenario {scenarioName}"`.
201+ - use custom `scenarioDefaultDescription` at `dbModel`.
202+ - use custom `description` for individual scenario.
203+
204+ 1. Example with default setting.
205+ ` ` ` yaml
206+ Invoice:
207+ type: object
208+ x-scenarios:
209+ - name: create
210+ - name: update
211+ ` ` `
212+
213+ The following code is generated in the abstract model :
214+ ` ` ` php
215+ abstract class Invoice extends \y ii\d b\A ctiveRecord
216+ {
217+ /**
218+ * Scenario create
219+ */
220+ public const SCENARIO_CREATE = 'create';
221+
222+ /**
223+ * Scenario update
224+ */
225+ public const SCENARIO_UPDATE = 'update';
226+
227+ /**
228+ * Automatically generated scenarios from the model 'x-scenarios'.
229+ * @return array a list of scenarios and the corresponding active attributes.
230+ */
231+ public function scenarios()
232+ {
233+ $parentScenarios = parent::scenarios();
234+
235+ /**
236+ * Each scenario is assigned attributes as in the 'default' scenario.
237+ * The advantage is that the scenario can be used immediately.
238+ * This can be overridden in the child model if needed.
239+ */
240+ $default = $parentScenarios[self::SCENARIO_DEFAULT];
241+
242+ return [
243+ self::SCENARIO_CREATE => $default,
244+ self::SCENARIO_UPDATE => $default,
245+ /**
246+ * The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
247+ * are automatically included in the scenarios() function for the model.
248+ */
249+ ...$parentScenarios,
250+ ];
251+ }
252+ }
253+ ` ` `
254+
255+ 2. Example with custom `description` for individual scenario.
256+ ` ` ` yaml
257+ Invoice:
258+ type: object
259+ x-scenarios:
260+ - name: create
261+ description: My custom description for scenario create
262+ - name: update
263+ ` ` `
264+
265+ The following code is generated in the abstract model :
266+ ` ` ` php
267+ abstract class Invoice extends \y ii\d b\A ctiveRecord
268+ {
269+ /**
270+ * My custom description for scenario create
271+ */
272+ public const SCENARIO_CREATE = 'create';
273+
274+ /**
275+ * Scenario update
276+ */
277+ public const SCENARIO_UPDATE = 'update';
278+
279+ /**
280+ * Automatically generated scenarios from the model 'x-scenarios'.
281+ * @return array a list of scenarios and the corresponding active attributes.
282+ */
283+ public function scenarios()
284+ {...}
285+ }
286+ ` ` `
287+
288+ 3. Example with custom `scenarioDefaultDescription`.
289+
290+ Set custom `scenarioDefaultDescription` at `dbModel`.
291+ `scenarioDefaultDescription` Accepted-Placeholder : {scenarioName}, {scenarioConst}, {modelName}.
292+
293+ For example, for the `create` scenario in the `Invoice` model, the placeholders would result in the following :
294+ ` {scenarioName}` = `create`
295+ ` {scenarioConst}` = `SCENARIO_CREATE`
296+ ` {modelName}` = `Invoice`
297+
298+ php-config-settings
299+ ` ` ` php
300+ $config['modules']['gii']['generators']['api'] = [
301+ 'class' => \c ebe\y ii2openapi\g enerator\A piGenerator::class,
302+ 'dbModel' => [
303+ /** Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
304+ 'scenarioDefaultDescription' => "This scenario \" {scenarioName}\" at Model \" {modelName}\" has Constant {scenarioConst}.",
305+ ],
306+ ...
307+ ];
308+ ` ` `
309+
310+ ` ` ` yaml
311+ Invoice:
312+ type: object
313+ x-scenarios:
314+ - name: create
315+ description: My custom description for scenario create
316+ - name: update
317+ ` ` `
318+
319+ The following code is generated in the abstract model :
320+ ` ` ` php
321+ abstract class Invoice extends \y ii\d b\A ctiveRecord
322+ {
323+ /**
324+ * My custom description for scenario create
325+ */
326+ public const SCENARIO_CREATE = 'create';
327+
328+ /**
329+ * This scenario "update" at Model "Invoice" has Constant SCENARIO_UPDATE.
330+ */
331+ public const SCENARIO_UPDATE = 'update';
332+
333+ /**
334+ * Automatically generated scenarios from the model 'x-scenarios'.
335+ * @return array a list of scenarios and the corresponding active attributes.
336+ */
337+ public function scenarios()
338+ {...}
339+ }
340+ ` ` `
341+
342+ 4. Example with custom `scenarioDefaultDescription`
343+ and use-case : both '\cebe\yii2openapi\generator\ApiGenerator::class' and '\common\client_generator\{your_ApiClientGenerator}::class' are used.
344+
345+ Set custom `scenarioDefaultDescription` at `dbModel`.
346+ `scenarioDefaultDescription` Accepted-Placeholder : {scenarioName}, {scenarioConst}, {modelName}.
347+
348+ php-config-settings
349+ ` ` ` php
350+ $config['modules']['gii']['generators']['api'] = [
351+ 'class' => \c ebe\y ii2openapi\g enerator\A piGenerator::class,
352+ 'dbModel' => [
353+ /** Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
354+ 'scenarioDefaultDescription' => implode("\n ", [
355+ "This Backend-Scenario \" {scenarioName}\" exist in both the frontend model and the backend model.",
356+ "@see \c ommon\c lient\m odels\{ modelName}::{scenarioConst}",
357+ ]),
358+ ],
359+ ...
360+ ];
361+
362+ $config['modules']['gii']['generators']['api-client'] = [
363+ 'class' => \c ommon\c lient_generator\{ your_ApiClientGenerator}::class,
364+ 'dbModel' => [
365+ /** AcceptedInputs: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
366+ 'scenarioDefaultDescription' => implode("\n ", [
367+ "This Frontend-Scenario \" {scenarioName}\" exist in both the frontend model and the backend model.",
368+ "@see \c ommon\m odels\b ase\{ modelName}::{scenarioConst}",
369+ ]),
370+ ],
371+ ...
372+ ` ` `
373+
374+ ` ` ` yaml
375+ Invoice:
376+ type: object
377+ x-scenarios:
378+ - name: create
379+ - name: update
380+ ` ` `
381+
382+ The following code is generated in the abstract model :
383+ ` ` ` php
384+ abstract class Invoice extends \y ii\d b\A ctiveRecord
385+ {
386+ /**
387+ * This Backend-Scenario "create" exist in both the frontend model and the backend model.
388+ * @see \c ommon\c lient\m odels\I nvoice::SCENARIO_CREATE
389+ */
390+ public const SCENARIO_CREATE = 'create';
391+
392+ /**
393+ * This Backend-Scenario "update" exist in both the frontend model and the backend model.
394+ * @see \c ommon\c lient\m odels\I nvoice::SCENARIO_UPDATE
395+ */
396+ public const SCENARIO_UPDATE = 'update';
397+
398+ /**
399+ * Automatically generated scenarios from the model 'x-scenarios'.
400+ * @return array a list of scenarios and the corresponding active attributes.
401+ */
402+ public function scenarios()
403+ {...}
404+ }
405+ ` ` `
406+
189407# ## `x-indexes`
190408
191409Specify table indexes
@@ -350,7 +568,7 @@ related objects, `x-no-relation` (type: boolean, default: false) is used.
350568
351569This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in Yii model file.
352570
353- In order to make it real database column, extension `x-no-relation` can be used.
571+ In order to make it real database column, OpenAPI extension `x-no-relation` can be used.
354572
355573` ` ` yaml
356574 comments:
0 commit comments