cakephp多选不起作用

时间:2016-08-21 05:34:32

标签: cakephp cakephp-3.2 validates-associated

我有两张桌子' business'和business_categories和他们的关联就像

BusinessesTable.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-&gt; request-&gt; 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'

}

],

1 个答案:

答案 0 :(得分: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

关注我的代码。当我调试时,它将显示如下(有越南城市)

这是我的数据库。 enter image description here

这是我查询后的调试。

[
    (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(值)在哪里以及文本在哪里。因为你的表中可能会有很多列(字段)。