2016-08-23 1 views
0

カラム名の複数の翻訳を1つのフォームに保存しようとしていますが、常に 'name'にデフォルト値がありません。以下は、cakephpの最新のドキュメントに従った実装です。テーブルの言葉Cakephp 3は同時に複数の翻訳を保存することができません

CREATE TABLE `word_i18n` (
`id` int(10) NOT NULL AUTO_INCREMENT, 
`locale` varchar(6) NOT NULL, 
`model` varchar(255) NOT NULL, 
`foreign_key` int(10) NOT NULL, 
`field` varchar(255) NOT NULL, 
`content` mediumtext, 
PRIMARY KEY (`id`), 
KEY `locale` (`locale`), 
KEY `model` (`model`), 
KEY `row_id` (`foreign_key`), 
KEY `field` (`field`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

を追加しましたWordsTable

への翻訳の行動のためのすべての翻訳を保持

CREATE TABLE `words` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`name` varchar(255) NOT NULL, 
`slug` varchar(255) NOT NULL, 
`created` datetime NOT NULL, 
`modified` datetime NOT NULL, 
PRIMARY KEY (`id`), 
UNIQUE KEY `slug` (`slug`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

表のword_i18n構造単語の

テーブル構造翻訳形質

class Word extends Entity 
{ 
    use TranslateTrait; 

    /** 
    * Fields that can be mass assigned using newEntity() or patchEntity(). 
    * 
    * Note that when '*' is set to true, this allows all unspecified fields to 
    * be mass assigned. For security purposes, it is advised to set '*' to false 
    * (or remove it), and explicitly make individual fields accessible as needed. 
    * 
    * @var array 
    */ 
    protected $_accessible = [ 
     '*' => true, 
     'id' => false 
    ]; 
} 

コントローラメソッドで0

Wordのエンティティがデータ

<?= $this->Form->create($word); ?> 
    <fieldset> 
     <legend><?= __('Add Word') ?></legend> 
     <?php 
      echo $this->Form->input('_translations.en.name',['class'=>"form-control ui-flat", "label" => __("Name [{0}]", ["English"])]); 
      echo $this->Form->input('_translations.ja.name',['class'=>"form-control ui-flat", "label" => __("Name [{0}]", ["Japanese"]) ]); 
      echo $this->Form->input('_translations.ko.name',['class'=>"form-control ui-flat", "label" => __("Name [{0}]", ["Korean"])]); 
      echo $this->Form->input('_translations.zh.name',['class'=>"form-control ui-flat", "label" => __("Name [{0}]", ["Chinese"])]); 
      echo $this->Form->button(__('Submit'),array('class'=>"btn btn-success ui-flat pull-right")); 
     ?> 
    </fieldset> 
<?= $this->Form->end() ?> 
を提出する提出

public function add() 
{ 
    I18n::locale("en"); // Sets the default locale 
    $word = $this->Words->newEntity(); 
    if ($this->request->is('post')) { 
     $word = $this->Words->patchEntity($word, $this->request->data, ['translations' => true]); 
     //debug($word);die; 
     if ($this->Words->save($word)) { 
      $this->Flash->success(__('The word has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The word could not be saved. Please, try again.')); 
     } 
    } 
    $this->set(compact('word')); 
    $this->set('_serialize', ['word']); 
} 

そして最後にフォームをレンダリングし、処理します

すべてはCakePHPのドキュメントを実装するが、常にフィールドの検証エラーを得た名前は、このフィールドは を要求され、名前最初のフォームフィールドから_translations.enを削除して送信する場合には、検証が、リードを渡し_requiredですSQLエラーフィールド 'name'にはデフォルト値がありません。

答えて

関連する問題