2016-08-24 49 views
1

我有兩個表seller_businessesseller_business_categories。和他們的關聯如下在CakePHP中有多個select有hasMany關聯3

SellerBusinessesTable.php

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

SellerBusinessCategories.php

$this->belongsTo('SellerBusinesses', [ 
    'foreignKey' => 'seller_business_id', 
    'joinType' => 'INNER' 
]); 

我使用一個單一的形式將數據保存在兩個表中

<?= $this->Form->create($sellerBusiness, ['type' => 'file']) ?> 
<?= $this->Form->input('company_name') ?> 
<?= $this->Form->input('proof', ['type' => 'file']) ?> 
<?= $this->Form->input('seller_business_categories._category_ids', ['multiple' => true, 'options' => $categories]) ?> 
<?= $this->Form->button('submit', ['type' => 'submit']) ?> 
<?= $this->Form->end() ?> 

和控制器的作用是

$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' 
    ] 
]); 
debug($sellerBusiness); 
if ($save_s_b = $this->SellerBusinesses->save($sellerBusiness)) { 
    debug($save_s_b); 
    $this->Flash->success(__('The seller business has been saved.')); 

    return $this->redirect(['controller' => 'SellerBusinesses', 'action' => 'view', $save_s_b->id]); 
    } else { 
    $this->Flash->error(__('The seller business could not be saved. Please, try again.')); 
    } 
} 

但這不是保存記錄seller_business_categories表。

從文件Here

// Multiple select element for belongsToMany 
echo $this->Form->input('tags._ids', [ 
    'type' => 'select', 
    'multiple' => true, 
    'options' => $tagList, 
]); 

但是,這是行不通的。是否有hasMany

debug($this->request->data);任何其他方式給出

'seller_business_categories' => [ 
     '_category_ids' => [ 
      (int) 0 => '1', 
      (int) 1 => '2' 
     ] 
    ], 

object(App\Model\Entity\SellerBusiness) { 

    'seller_id' => (int) 16, 
    'company_name' => 'My company', 
    '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' 

     } 
    ], 
    '[new]' => true, 
    '[accessible]' => [ 
     '*' => true, 
    ], 
    '[dirty]' => [ 
     'seller_id' => true, 
     'company_name' => true, 
     'seller_business_categories' => true, 
    ], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'SellerBusinesses' 

} 

debug($sellerBusiness);patchEntity和錯誤

Error: SQLSTATE[HY000]: 
General error: 1364 Field 'category_id' doesn't have a default value in seller_business_categories table 
+0

你必須設置許多-to-many關聯,如果你想使用多選的形式, – Salines

+0

如何重寫'hasMany'?在設置「多對多」之前,我是否必須刪除'hasMany'? – Gaurav

+0

一個賣家可以有多個業務,多個業務屬於一個賣家。這就是爲什麼'hasMany'關係在那裏。沒有其他方法可以做到嗎? – Gaurav

回答

0

SellerBusinessesTable

$this->belongsToMany('Categories'); 

CategoriesTable

$this->belongsToMany('SellerBusinesses', [ 
    'foreignKey' => 'category_id', 
    'targetForeignKey' => 'seller_business_id', 
    'joinTable' => 'categories_seller_businesses' 
]); 

//透視表必須按照字母順序排序

​​

CategoriesSellerBusinessesTable

$this->belongsTo('SellerBusinesses', [ 
    'foreignKey' => 'seller_business_id', 
    'joinType' => 'INNER' 
]); 

$this->belongsTo('Categories', [ 
    'foreignKey' => 'category_id', 
    'joinType' => 'INNER' 
]); 

Add.ctp

<?= $this->Form->create($sellerBusiness, ['type' => 'file']) ?> 
    ... 
    <?= $this->Form->input('categories', ['type' => 'select', 'options' => $categories, 'multiple' => 'select', 'label' => __('Categories')]) ?> 
    ... 
<?= $this->Form->end() ?> 

SellerBusinessesController

public function add() 
{ 
    $sellerBusiness = $this->SellerBusinesses->newEntity(); 

    if ($this->request->is('post')) { 

     $sellerBusiness = $this->SellerBusinesses->patchEntity($sellerBusiness, $this->request->data); 

     $sellerBusiness->seller_id = $this->Auth->user('id'); 

     if(isset($this->request->data['categories'])) 
     { 
      $sellerBusiness -> categories = $this->SellerBusinesses->Categories->find()->where(['id IN' => $this->request->data['categories']])->all()->toArray(); 
     } 

     if ($this->SellerBusinesses->save($sellerBusiness)) { 

      $this->Flash->success(__('The sellerBusiness has been saved.')); 

      return $this->redirect(['action' => 'index']); 

     } else { 

      $this->Flash->error(__('The seller could not be saved. Please, try again.')); 

     } 
    } 

    $categories = $this -> SellerBusinesses-> Categories-> find('treeList'); 

    $this->set(compact('sellerBusiness', 'categories')); 
    $this->set('_serialize', ['sellerBusiness']);