自動生成機能自体に含まれていないため、私はJavascriptで回避策を構築しています。
重要:このソリューションは、ユーザーがyiiアドバンステンプレートを使用しており、バックエンドアプリケーションからGiiを使用していることを前提としています。この場合、コードを調整する必要があります
まず、私のJSスクリプトはgii/templates/crud/form.php
に含まれていますが、Giiテンプレートを設定した場所によって、この宛先が異なる場合があります。
\backend\assets\CrudAutocompleteAsset::register($this);
echo $form->field($generator, 'templateDestination')->dropDownList([
"backend" => "Backend",
"frontend" => "Frontend"
]);
また、あなたのGenerator
クラスにパブリックプロパティtemplateDestination
を含める必要があります。拡張テンプレートを使用しない場合は、これも選択フィールドも必要ありません。すると、アセットが必要になります。あなたは資産を使用する方法がわからない場合は、Yii2 guide about Assets
/**
* Destination namespace of generated code for advanced yii template
* @var string
*/
public $templateDestination = "backend";
資産CrudAutocompleteAsset
意志の広告私のjsファイルをチェックしてください。私は作った
$(document).ready(function() {
var templateDestinationSelect = $("select[name='Generator[templateDestination]']");
var modelClassInput = $("input[name='Generator[modelClass]']");
var searchModelClassInput = $("input[name='Generator[searchModelClass]']");
var controllerClassInput = $("input[name='Generator[controllerClass]']");
var viewPathInput = $("input[name='Generator[viewPath]']");
modelClassInput.change(function() {
var destination = templateDestinationSelect.val();
var modelClass = $(this).val();
var modelNs = modelClass.substr(0, modelClass.lastIndexOf("\\"));
var targetNs = destination + modelNs.substr(modelNs.indexOf("\\"));
var modelClassName = modelClass.substr(modelClass.lastIndexOf("\\") + 1);
var modelViewName = modelClassName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
var modelSearchClass = modelNs + "\\" + modelClassName + "Search";
searchModelClassInput.val(modelSearchClass);
var controllerClassName = targetNs.replace("models", "controllers") + "\\" + modelClassName + "Controller";
controllerClassInput.val(controllerClassName);
var viewPath = "@app/../" + destination + "views/" + modelViewName;
viewPathInput.val(viewPath);
})
私の質問は、私はすでに私のテンプレートとジェネレータがあります。問題は、Giiのバックエンドにオートコンプリート値を追加する方法でした。これは、ジェネレータによって構築されるためです – StoryTeller