如何保存与翻译的关联? CakePHP 3.1

时间:2015-11-27 15:55:24

标签: cakephp orm cakephp-3.0

我有一个数组

 \src\Controller\Admin\FaqsController.php (line 34)

[
    'Faqs' => [
        'en' => [
            'question' => 'sd ewg',
            'answer' => 'ewwgf iwegewgenrjtrjtjt'
        ],
        'ar' => [
            'question' => 'vdsvdsv',
            'answer' => 'dvdsvdsvds'
        ]
    ],
    'SeoManages' => [
        'model_name' => 'Faqs',
        'en' => [
            'title' => '',
            'slug' => '',
            'description' => '',
            'keywords' => ''
        ],
        'ar' => [
            'title' => 'vdsvdsv',
            'slug' => 'vdsvds',
            'description' => 'dvsdvds s',
            'keywords' => 'few,vergerhxv,benev'
        ]
    ]
]

我的模特

常见问题     

class FaqsTable extends AppTable {
    public function initialize(array $config)
    {
        $this->addBehavior('Translate', [
            'fields' => ['question', 'answer'],
            'translationTable' => 'FaqsI18n'
        ]);
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created' => 'new',
                    'modified' => 'always'
                ]
            ]
        ]);

        $this->hasOne('SeoManages', [
            'className' => 'SeoManages',
            'foreignKey' => 'field_id',
            'conditions' => ['SeoManages.model_name' => 'Faqs'],
            'dependent' => true,
        ]);
    }
}

SeoManages

<?php
namespace App\Model\Table;
use Cake\ORM\Table;

class SeoManagesTable extends Table {

    public function initialize(array $config)
    {
        $this->addBehavior('Translate', [
            'fields' => ['title', 'prep_title', 'append_title' ,'description', 'keywords', 'slug'],
            'translationTable' => 'SeoManagesI18n'
        ]);
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created' => 'new',
                    'modified' => 'always'
                ]
            ]
        ]);
    }
}

控制器中的方法

public function add()
    {
        if ($this->request->is('post')) {
            // var_dump($this->request->data); exit;
            $faqs = $this->Faqs->newEntity();
            $translations = $this->request->data['Faqs'];

            foreach ($translations as $lang => $data) {
                $faqs->translation($lang)->set($data, ['guard' => false]);
            }

            if ($this->Faqs->save($faqs, ['associated' => 'SeoManages'])) {
                $this->Flash->set('The FAQ saved succssesfully.', ['element' => 'HCMS/success']);
                return $this->redirect('/admin/Faqs/index');
            } else {
                $this->Flash->set('The FAQ can not be saved.', ['element' => 'HCMS/error']);
                return $this->redirect('/admin/Faqs/index');
            }
        }
    }

当我保存数据时,我没有任何错误。与Faqs相关的数据保存,但Seo数据没有。 如何保存这些数据? 当然,我可以逐个保存模型中的数据,但它不是解决方案。

0 个答案:

没有答案