1

I have two tables 'businesses' and business_categories and their association is like

BusinessesTable.php

$this->hasMany('SellerBusinessCategories', [
    foreignKey => 'business_id'
]);

and I have to input multiple categories to business_categories table along with businesses.

This is how the input field is in add.ctp view

<?= $this->Form->input('seller_business_categories._category_ids', [
        'options' => $categories,
        'multiple' => true,
        'type' => 'select',
        'class' => 'form-control select2',
        'label' => false
    ])
?>

but this is giving error as

Error: SQLSTATE[HY000]: General error:
1364 Field 'category_id' doesn't have a default value in business_categories

and form is not submitting. Removing multiple => true and replacing business_categories._category_ids to business.category_id is working fine.

Is there anything missing ?

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'

}

],

4

1 回答 1

0

我认为您获取列表的查询不够清楚

<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

按照我的代码。当我调试它会显示如下(有越南城市)

这是我的数据库。 在此处输入图像描述

这是我查询后的调试。

[
    (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',
]

您必须定义选择框知道id(值)在哪里以及文本在哪里。因为可能在你的表中会有很多列(字段)。

于 2016-08-23T02:44:02.940 回答