2017-06-07 16 views
1

私は記事とタグでブログを作ろうとしますが、何らかの理由で記事作成時にタグが保存されません。 私は公式のケーキのブログからいくつかのアイデアに従っていたが、ここでは運がない。私が間違っていると私は見ていないかもしれない。キーワードのCakephp 3のセーブアソシエーションが動作しない

テーブル

CREATE TABLE `keywords` (
    `id` int(11) NOT NULL, 
    `article_id` int(11) DEFAULT NULL, 
    `tag` varchar(255) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

表モデル キーワード:

class KeywordsTable extends Table 
{ 
...................... 
    public function initialize(array $config) 
    { 
     parent::initialize($config); 

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

     $this->belongsTo('Article', [ 
      'foreignKey' => 'article_id', 
      'joinType' => 'INNER' 
     ]); 
    } 
.................... 
} 

class ArticleTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     parent::initialize($config); 

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

     $this->addBehavior('Timestamp'); 

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

     $this->hasMany('Keywords', [ 
      'foreignKey' => 'article_id' 
     ]); 
    } 
...................................... 
} 

そして、ここでは私のコントローラである:

public function add() 
{ 

     //$this->autoRender = false; 
     $article = $this->Article->newEntity(); 


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

     $article = $this->Article->patchEntity($article, $this->request->data, ['associated'=>['Keywords']]); 
     echo "<pre>"; 
     print_r($this->Article->save($article)); 
     die(); 
     } 
} 

そして、このいずれかが、私は感謝します私を助けることができるのであれば、私の形で私が使用するコード

<?= $this->Form->input('article[keywords][]', ['label'=>false, 'class' => 'form-control', 'placeholder' => 'keywords', 'templates' => ['inputContainer' => '{{content}}']]); ?> 

です。

答えて

0

4時間後、私は問題を見つけることができました。 ビューのフォーム入力に問題がありました。

それは代わりに記事のarticle.0.tagされている必要があります[キーワード] [] はそれを変更し、すべてが動作するようになりました。

よろしくコントローラで

0

あなたが連想配列を使用する必要があり、more details - このような

$this->Article->save($article,['associated' => ['Keywords']]); 

とビューファイルで、作るの入力フィールド -

<?= $this->Form->input('keywords.0.title', ['label'=>false, 'class' => 'form-control', 'placeholder' => 'keywords', 'templates' => ['inputContainer' => '{{content}}']]); ?> 

more details

関連する問題