cakephp 多个 select 不工作
cakephp multiple select not working
我有两个 tables 'businesses' 和 business_categories
他们的关联就像
BusinessesTable.php
$this->hasMany('SellerBusinessCategories', [
foreignKey => 'business_id'
]);
我必须输入多个类别 business_categories
table 以及 businesses
.
这是输入字段在 add.ctp
视图中的样子
<?= $this->Form->input('seller_business_categories._category_ids', [
'options' => $categories,
'multiple' => true,
'type' => 'select',
'class' => 'form-control select2',
'label' => false
])
?>
但这给出了错误,因为
Error: SQLSTATE[HY000]: General error:
1364 Field 'category_id' doesn't have a default value in business_categories
并且表单未提交。删除 multiple => true
并将 business_categories._category_ids
替换为 business.category_id
工作正常。
有什么遗漏吗?
Edit 2
SellerBusinessesController.php
public function add()
{
$sellerBusiness = $this->SellerBusinesses->newEntity();
if ($this->request->is('post')) {
$sellerBusiness->seller_id = $this->Auth->user('id');
$sellerBusiness = $this->SellerBusinesses->patchEntity($sellerBusiness, $this->request->data, [
'associated' => [
'SellerBusinessCategories'
]
]);
if ($this->SellerBusinesses->save($sellerBusiness)) {
$this->Flash->success(__('The seller business has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The seller business could not be saved. Please, try again.'));
}
}
$categories = $this->SellerBusinesses->SellerBusinessCategories->Categories->find('list', ['limit' => 200]);
$sellers = $this->SellerBusinesses->Sellers->find('list', ['limit' => 200]);
$this->set(compact('sellerBusiness', 'sellers', 'categories'));
$this->set('_serialize', ['sellerBusiness']);
}
on debugging : debug($this->request->data), gives
'seller_business_categories' => [
(int) 0 => object(App\Model\Entity\SellerBusinessCategory) {
(int) 0 => '1',
(int) 1 => '2',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
(int) 0 => true,
(int) 1 => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'SellerBusinessCategories'
}
],
我认为您获取列表的查询不够明确
<select>
<option value="$AAA">$BBB</option>
</select>
您需要将 keyField 作为值 ($AAA) 并将 valueField 作为文本视图 ($BBB)。
这是我为下拉列表获取城市列表的示例。
public function getcity()
{
//Get model MCities
$MCities = TableRegistry::get('MCities');
$query = $MCities->find('list', [
'keyField' => 'id', //consider this line
'valueField' => 'city',//consider this line
// 'order' => 'city'
]);
//Add the default zero is "Select city"
$data_init = ['0'=>'Select city'];
$data = $data_init + $query->toArray();
return $data;
}
希望对您有所帮助!
编辑 #2
遵循我的代码。当我调试时它会显示如下(有越南城市)
这是我的数据库。
这是我查询后的debug
[
(int) 0 => 'Select city',
(int) 1 => 'Thành phố Hà Nội',
(int) 2 => 'Tỉnh Hà Giang',
(int) 3 => 'Tỉnh Cao Bằng',
(int) 4 => 'Tỉnh Bắc Kạn',
(int) 5 => 'Tỉnh Tuyên Quang',
(int) 6 => 'Tỉnh Lào Cai',
(int) 7 => 'Tỉnh Điện Biên',
(int) 8 => 'Tỉnh Lai Châu',
(int) 9 => 'Tỉnh Sơn La',
(int) 10 => 'Tỉnh Yên Bái',
]
您必须定义 select box 知道 id(值)在哪里以及文本在哪里。因为也许在您的 table 中会有很多列(字段)。
我有两个 tables 'businesses' 和 business_categories
他们的关联就像
BusinessesTable.php
$this->hasMany('SellerBusinessCategories', [
foreignKey => 'business_id'
]);
我必须输入多个类别 business_categories
table 以及 businesses
.
这是输入字段在 add.ctp
视图中的样子
<?= $this->Form->input('seller_business_categories._category_ids', [
'options' => $categories,
'multiple' => true,
'type' => 'select',
'class' => 'form-control select2',
'label' => false
])
?>
但这给出了错误,因为
Error: SQLSTATE[HY000]: General error:
1364 Field 'category_id' doesn't have a default value in business_categories
并且表单未提交。删除 multiple => true
并将 business_categories._category_ids
替换为 business.category_id
工作正常。
有什么遗漏吗?
Edit 2
SellerBusinessesController.php
public function add()
{
$sellerBusiness = $this->SellerBusinesses->newEntity();
if ($this->request->is('post')) {
$sellerBusiness->seller_id = $this->Auth->user('id');
$sellerBusiness = $this->SellerBusinesses->patchEntity($sellerBusiness, $this->request->data, [
'associated' => [
'SellerBusinessCategories'
]
]);
if ($this->SellerBusinesses->save($sellerBusiness)) {
$this->Flash->success(__('The seller business has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The seller business could not be saved. Please, try again.'));
}
}
$categories = $this->SellerBusinesses->SellerBusinessCategories->Categories->find('list', ['limit' => 200]);
$sellers = $this->SellerBusinesses->Sellers->find('list', ['limit' => 200]);
$this->set(compact('sellerBusiness', 'sellers', 'categories'));
$this->set('_serialize', ['sellerBusiness']);
}
on debugging : debug($this->request->data), gives
'seller_business_categories' => [
(int) 0 => object(App\Model\Entity\SellerBusinessCategory) {
(int) 0 => '1',
(int) 1 => '2',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
(int) 0 => true,
(int) 1 => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'SellerBusinessCategories'
}
],
我认为您获取列表的查询不够明确
<select>
<option value="$AAA">$BBB</option>
</select>
您需要将 keyField 作为值 ($AAA) 并将 valueField 作为文本视图 ($BBB)。
这是我为下拉列表获取城市列表的示例。
public function getcity()
{
//Get model MCities
$MCities = TableRegistry::get('MCities');
$query = $MCities->find('list', [
'keyField' => 'id', //consider this line
'valueField' => 'city',//consider this line
// 'order' => 'city'
]);
//Add the default zero is "Select city"
$data_init = ['0'=>'Select city'];
$data = $data_init + $query->toArray();
return $data;
}
希望对您有所帮助!
编辑 #2
遵循我的代码。当我调试时它会显示如下(有越南城市)
这是我的数据库。
这是我查询后的debug
[
(int) 0 => 'Select city',
(int) 1 => 'Thành phố Hà Nội',
(int) 2 => 'Tỉnh Hà Giang',
(int) 3 => 'Tỉnh Cao Bằng',
(int) 4 => 'Tỉnh Bắc Kạn',
(int) 5 => 'Tỉnh Tuyên Quang',
(int) 6 => 'Tỉnh Lào Cai',
(int) 7 => 'Tỉnh Điện Biên',
(int) 8 => 'Tỉnh Lai Châu',
(int) 9 => 'Tỉnh Sơn La',
(int) 10 => 'Tỉnh Yên Bái',
]
您必须定义 select box 知道 id(值)在哪里以及文本在哪里。因为也许在您的 table 中会有很多列(字段)。