Search code examples
cakephpcakephp-3.2validates-associated

cakephp multiple select not working


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'

}

],


Solution

  • I thought that your query to get list was not clear enough

    <select>
     <option value="$AAA">$BBB</option>
    </select>
    

    You need to get keyField as the value ($AAA) and the valueField as text view ($BBB).

    This is my example for getting the city list for my Dropdown list.

    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;
    }
    

    Hope it help !

    EDIT #2

    Following my code. When I debug it will show as below (There are Vietnamese cities)

    This is my database. enter image description here

    And this is my debug after query.

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

    You have to define to select box knows that where is the id (value) and where is the text. Because of maybe in your table will have many columns (fields).