Commit 51b04828 by Paul Klimov

Merge pull request #3964 from klimov-paul/sphinx-gii

Enh: Sphinx Gii generator created
parents 194f451a 7e823f09
......@@ -17,6 +17,7 @@ Yii Framework 2 sphinx extension Change Log
- Enh #1398: Refactor ActiveRecord to use BaseActiveRecord class of the framework (klimov-paul)
- Enh #2002: Added filterWhere() method to yii\spinx\Query to allow easy addition of search filter conditions by ignoring empty search fields (samdark, cebe)
- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe)
- Enh #3964: Gii generator for Active Record model added (klimov-paul)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
- Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`.
All relational queries are now directly served by `ActiveQuery` allowing to use
......
......@@ -243,3 +243,27 @@ foreach ($articles as $article) {
echo $article->snippet;
}
```
Using Gii generator
-------------------
This extension provides a code generator, which can be integrated with yii 'gii' module. It allows generation of the
Active Record code. In order to enable it, you should adjust your application configuration in following way:
```php
return [
//....
'modules' => [
// ...
'gii' => [
'class' => 'yii\gii\Module',
'generators' => [
'sphinxModel' => [
'class' => 'yii\sphinx\gii\model\Generator'
]
],
],
]
];
```
\ No newline at end of file
<?php
/**
* This is the template for generating the model class of a specified Sphinx index.
*
* @var yii\web\View $this
* @var yii\sphinx\gii\model\Generator $generator
* @var string $indexName full table name
* @var string $className class name
* @var yii\sphinx\IndexSchema $indexSchema
* @var string[] $labels list of attribute labels (name => label)
* @var string[] $rules list of validation rules
*/
echo "<?php\n";
?>
namespace <?= $generator->ns ?>;
use Yii;
/**
* This is the model class for index "<?= $indexName ?>".
*
<?php foreach ($indexSchema->columns as $column): ?>
* @property <?= $column->isMva ? 'array' : $column->phpType ?> <?= "\${$column->name}\n" ?>
<?php endforeach; ?>
*/
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
{
/**
* @inheritdoc
*/
public static function indexName()
{
return '<?= $generator->generateIndexName($indexName) ?>';
}
<?php if ($generator->db !== 'sphinx'): ?>
/**
* @return \yii\sphinx\Connection the database connection used by this AR class.
*/
public static function getDb()
{
return Yii::$app->get('<?= $generator->db ?>');
}
<?php endif; ?>
/**
* @inheritdoc
*/
public function rules()
{
return [<?= "\n " . implode(",\n ", $rules) . "\n " ?>];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
<?php foreach ($labels as $name => $label): ?>
<?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?php endforeach; ?>
];
}
}
<?php
/**
* @var yii\web\View $this
* @var yii\widgets\ActiveForm $form
* @var yii\sphinx\gii\model\Generator $generator
*/
echo $form->field($generator, 'indexName');
echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'ns');
echo $form->field($generator, 'baseClass');
echo $form->field($generator, 'db');
echo $form->field($generator, 'useIndexPrefix')->checkbox();
echo $form->field($generator, 'enableI18N')->checkbox();
echo $form->field($generator, 'messageCategory');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment