2016-08-24 18 views
1

두 테이블 seller_businessesseller_business_categories이 있습니다. 다음과 협회는CakePHP 3에서 hasMany 연관을 여러 번 선택했습니다.

내가 두 테이블에 데이터를 저장하는 하나의 양식을 사용하고 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() ?> 
(210)

그리고 컨트롤러 액션은

$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' 
     ] 
    ], 

debug($sellerBusiness); 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' 

} 

및 오류

가 준다
+0

다음을 수행해야 설정의 many-to-many 연관은 다중 선택 형태, – Salines

+0

방법을 사용하려는 경우 'hasMany'를 오버라이드 하시겠습니까? 'many-to-many'를 설정하기 전에'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' 
]); 

피벗 테이블을 알파벳 순으로 정렬되어 있어야합니다 //

bin/cake bake migration CreateCategoriesSellerBusinesses seller_business_id:integer category_id:integer created modified 

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']); 
관련 문제