-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added return types for methods in models
- Loading branch information
Showing
3 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
/** | ||
* This is the template for generating the model class of a specified table. | ||
*/ | ||
|
||
/** @var $enum array list of ENUM fields */ | ||
/** @var yii\web\View $this */ | ||
/** @var yii\gii\generators\model\Generator $generator */ | ||
/** @var string $tableName full table name */ | ||
/** @var string $className class name */ | ||
/** @var string $queryClassName query class name */ | ||
/** @var yii\db\TableSchema $tableSchema */ | ||
/** @var array $properties list of properties (property => [type, name. comment]) */ | ||
/** @var string[] $labels list of attribute labels (name => label) */ | ||
/** @var string[] $rules list of validation rules */ | ||
/** @var array $relations list of relations (name => relation declaration) */ | ||
|
||
echo "<?php\n"; | ||
?> | ||
|
||
namespace <?= $generator->ns ?>; | ||
|
||
use \yii\db\Connection; | ||
/** | ||
* This is the model class for table "<?= $generator->generateTableName($tableName) ?>". | ||
* | ||
<?php foreach ($properties as $property => $data): ?> | ||
* @property <?= "{$data['type']} \${$property}" . ($data['comment'] ? ' ' . strtr($data['comment'], ["\n" => ' ']) : '') . "\n" ?> | ||
<?php endforeach; ?> | ||
<?php if (!empty($relations)): ?> | ||
* | ||
<?php foreach ($relations as $name => $relation): ?> | ||
* @property <?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?> | ||
<?php endforeach; ?> | ||
<?php endif; ?> | ||
*/ | ||
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?> | ||
{ | ||
|
||
<?php if (!empty($enum)): ?> | ||
/** | ||
* ENUM field values | ||
*/ | ||
<?php | ||
foreach($enum as $columnName => $columnData) { | ||
foreach ($columnData['values'] as $enumValue){ | ||
echo ' const ' . $enumValue['constName'] . ' = \'' . $enumValue['value'] . '\';' . PHP_EOL; | ||
} | ||
} | ||
endif | ||
?> | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function tableName() : string | ||
{ | ||
return '<?= $generator->generateTableName($tableName) ?>'; | ||
} | ||
<?php if ($generator->db !== 'db'): ?> | ||
|
||
/** | ||
* @return Connection the database connection used by this AR class. | ||
*/ | ||
public static function getDb() : Connection | ||
{ | ||
return Yii::$app->get('<?= $generator->db ?>'); | ||
} | ||
<?php endif; ?> | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rules() : array | ||
{ | ||
return [<?= empty($rules) ? '' : ("\n " . implode(",\n ", $rules) . ",\n ") ?>]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function attributeLabels() : array | ||
{ | ||
return [ | ||
<?php foreach ($labels as $name => $label): ?> | ||
<?= "'$name' => " . $generator->generateString($label) . ",\n" ?> | ||
<?php endforeach; ?> | ||
]; | ||
} | ||
<?php foreach ($relations as $name => $relation): ?> | ||
|
||
/** | ||
* Gets query for [[<?= $name ?>]]. | ||
* | ||
* @return <?= $relationsClassHints[$name] . "\n" ?> | ||
*/ | ||
public function get<?= $name ?>() : <?= $relationsClassHints[$name] ?> | ||
{ | ||
<?= $relation[0] . "\n" ?> | ||
} | ||
<?php endforeach; ?> | ||
<?php if ($queryClassName): ?> | ||
<?php | ||
$queryClassFullName = ($generator->ns === $generator->queryNs) ? $queryClassName : '\\' . $generator->queryNs . '\\' . $queryClassName; | ||
echo "\n"; | ||
?> | ||
/** | ||
* {@inheritdoc} | ||
* @return <?= $queryClassFullName ?> the active query used by this AR class. | ||
*/ | ||
public static function find() : <?= $queryClassFullName ?> | ||
{ | ||
return new <?= $queryClassFullName ?>(get_called_class()); | ||
} | ||
<?php endif; ?> | ||
|
||
<?php if ($enum): ?> | ||
<?php foreach ($enum as $columnName => $columnData): ?> | ||
|
||
/** | ||
* column <?= $columnName ?> ENUM value labels | ||
* @return string[] | ||
*/ | ||
public static function <?= $columnData['funcOptsName'] ?>() : array | ||
{ | ||
return [ | ||
<?php foreach ($columnData['values'] as $k => $value): ?> | ||
<?php | ||
if ($generator->enableI18N) { | ||
echo ' self::' . $value['constName'] . ' => Yii::t(\'' . $generator->messageCategory . '\', \'' . $value['value'] . "'),\n"; | ||
} else { | ||
echo ' self::' . $value['constName'] . ' => \'' . $value['value'] . "',\n"; | ||
} | ||
?> | ||
<?php endforeach; ?> | ||
]; | ||
} | ||
<?php endforeach; ?> | ||
<?php foreach ($enum as $columnName => $columnData): ?> | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function <?= $columnData['displayFunctionPrefix'] ?>() : string | ||
{ | ||
return self::<?= $columnData['funcOptsName'] ?>()[$this-><?=$columnName?>]; | ||
} | ||
<?php foreach ($columnData['values'] as $enumValue): ?> | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function <?= $columnData['isFunctionPrefix'] . $enumValue['functionSuffix'] ?>() : bool | ||
{ | ||
return $this-><?= $columnName ?> === self::<?= $enumValue['constName'] ?>; | ||
} | ||
|
||
public function <?= $columnData['setFunctionPrefix'] . $enumValue['functionSuffix'] ?>() | ||
{ | ||
$this-><?= $columnName ?> = self::<?= $enumValue['constName'] ?>; | ||
} | ||
<?php endforeach; ?> | ||
<?php endforeach; ?> | ||
<?php endif; ?> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters