2016-08-24 23 views
1

私は2つのテーブルseller_businessesseller_business_categoriesを持っています。以下のように、その関連があるCakePHP 3の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);patchEntity

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' 

} 

とエラー

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

debug($sellerBusiness);を与えるために他の方法はあります

+0

あなたは複数選択フォームを使用したい場合は、 – Salines

+0

方法'hasMany'を上書きする? 'many-to-many'を設定する前に' hasMany'を削除しなければなりませんか? – Gaurav

+0

1つの売り手は複数のビジネスを持ち、複数のビジネスは1つの売り手に属します。これは、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']); 
関連する問題