2017-04-18 4 views
0

Ich habe zwei Tabellen articles und tags und dritte Zuordnungstabelle articles_tags jetzt möchte ich Daten in articles_tags speichern, wenn neue Artikel erstellen und Benutzer mehrere Tags aus Drop wählen unten mein Controller-Code unten istDaten speichern mit Belong zu viele cakehp 3.x

public function add() 
{ 
    $article = $this->Articles->newEntity(); 
    if ($this->request->is('post')) { 
      $article = $this->Articles->patchEntity($article, $this->request->data, [ 
      'associated' => ['Tags'] 
      ]); 

     if ($this->Articles->save($article)) { 
      $this->Flash->success(__('The article has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The article could not be saved. Please, try again.')); 
     } 
    } 

    $tags = $this->Articles->Tags->find('list',[ 
              'keyField' => 'id', 
              'valueField' => 'name' 
             ]); 

    $this->set(compact('article','tags')); 
    $this->set('_serialize', ['article','tags']); 
} 

und meine Ansicht Code ist unten

<?php 
    $this->Form->create($article); 
    echo $this->Form->input('title'); 
    echo $this->Form->input('status'); 
    echo $this->Form->select('tags.tag_id', 
           $tags, 
           ['empty' => '(choose one)', 'multiple' => true,] 
          ); 
    echo $this->Form->button(__('Submit')); 
    echo $this->Form->end(); 
?> 

und mein Artikel und Tag-Modell-Code ist unter

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('articles'); 
    $this->displayField('title'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 
    $this->belongsToMany('Tags', [ 
     'foreignKey' => 'article_id', 
     'targetForeignKey' => 'tag_id', 
     'joinTable' => 'articles_tags' 
    ]); 
} 


public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('tags'); 
    $this->displayField('name'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsToMany('Articles', [ 
     'foreignKey' => 'tag_id', 
     'targetForeignKey' => 'article_id', 
     'joinTable' => 'articles_tags' 
    ]); 
} 

aber wenn ich zum Speichern von Daten als Daten in Artikel nur speichern Tabelle nur im Zusammenhang Tag-ID und Artikel-ID nicht in article_tags Tabelle Speichern weiß nicht, was das Problem ist, bitte helfen Sie mir

Dank

Antwort

0

korrigieren Sie Ihre Code anzeigen

<?php 
$this->Form->create($article); 
echo $this->Form->input('title'); 
echo $this->Form->input('status'); 
echo $this->Form->select('tags._ids', 
          $tags, 
          ['empty' => '(choose one)', 'multiple' => true,] 
         ); 
echo $this->Form->button(__('Submit')); 
echo $this->Form->end(); 

?>

Verwandte Themen