我正在使用Backpack for Laravel作为管理实体的后端 . 我有一个'show'实体,定义如下:
public function up()
{
Schema::create('shows', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->nullable();
$table->string('title');
// Created/modified timestamps
$table->timestamps();
});
// Add our search indexes
DB::statement('ALTER TABLE shows ADD searchable tsvector NULL');
DB::statement('CREATE INDEX shows_index ON shows USING GIN (searchable)');
}
正如你所看到的那样,有一个'searchable'列是一个tsvector . 这对我的Postgres FTS很重要 .
在show实体的CrudController中,我手动为我的title和category_id字段定义以下内容:
// Title
$this->crud->addField([
'name' => 'title',
'label' => 'Show Title',
'type' => 'text'
]);
$this->crud->addColumn([
'name' => 'title',
'label' => 'Title'
]);
// Assigned category ID
$this->crud->addField([ // Select2Multiple = n-n relationship (with pivot table)
'label' => "Assign to Category",
'type' => 'select2',
'name' => 'category_id', // the db column for the foreign key
'entity' => 'category', // the method that defines the relationship in your Model
'attribute' => 'title', // property from the model that is shown to user
'model' => "App\Models\Category", // foreign key model
'data_source' => url("admin/category")
]);
$this->crud->addColumn([
'name' => 'category_id', // The db column name
'label' => "Category", // Table column heading
'type' => 'select',
'entity' => 'category',
'model' => "App\Models\Category",
'attribute' => 'title'
]);
问题是category_id字段 . 虽然它与可搜索的tsvector字段无关,但似乎'select2'类型出于某种原因查看了我的所有列,看到了我的'可搜索'列的tsvector类型,并且在我编辑节目的任何时候打破 . 我可以在Backpack中看到show index页面就好了,但是当我在特定节目上点击编辑时,我收到以下错误:
请求未知的数据库类型tsvector,Doctrine \ DBAL \ Platforms \ PostgreSQL92Platform可能不支持它 . (查看:resources / views / vendor / backpack / crud / fields / select2.blade.php)
如果我试着将category_id字段显示为一个简单的文本类型,我没有错误,它可以正常工作:
$this->crud->addField([
'name' => 'category_id',
'label' => 'Category ID',
'type' => 'text'
]);
$this->crud->addColumn([
'name' => 'category_id',
'label' => 'Category ID'
]);
虽然我在我的CrudController中手动定义我的字段,而不是从数据库中提取每个字段,虽然我没有在这里为select2引用我的'searchable'字段,但它仍然很奇怪看着我的“可搜索”栏目,看到了tsvector类型,并放弃了我 .
EDIT :
我已经将问题追溯到resources / views / vendor / backpack / crud / fields / select2.blade.php,特别是这段代码:
@if ($entity_model::isColumnNullable($field['name']))
<option value="">-</option>
@endif
如果我评论这三行,一切都很好 . 尽管$ field ['name']是'category_id'而不是tsvector'searchable'字段,但由于某种原因检查它是否可以为空可以打破整个事情 . 我还没有进一步追踪这一点 .
1 回答
我自己为此找到了解决方案 . 由于学说不知道tsvector类型,它需要在vendor / backpack / crud / src / CrudTrait.php中注册
在isColumnNullable()函数中,已经有一个不同的不支持字段类型(枚举)被强制转换为字符串的示例:
之后我简单地添加了tsvector类型,现在一切正常: