私は2つのテーブル「ビジネス」を持っておりbusiness_categories
、それらの関連付けは次のようになります
ビジネステーブル.php
$this->hasMany('SellerBusinessCategories', [
foreignKey => 'business_id'
]);
business_categories
と一緒にテーブルに複数のカテゴリを入力する必要があります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
ています。
足りないものはありますか?
編集 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']);
}
デバッグ時: debug($this->request->data)、
'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'
}
]、