1

我在使用 Yii2 多语言行为时遇到问题,希望这里有人可以帮助我。

这是我的数据库

CREATE TABLE IF NOT EXISTS `post` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 `enabled` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `postLang` (
`id` int(11) NOT NULL AUTO_INCREMENT,
 `post_id` int(11) NOT NULL,
 `language` varchar(6) NOT NULL,
 `title` varchar(255) NOT NULL,
 `content` TEXT NOT NULL,
 PRIMARY KEY (`id`),
 KEY `post_id` (`post_id`),
 KEY `language` (`language`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

ALTER TABLE `postLang`
ADD CONSTRAINT `postlang_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

而这个 PostlangController

use omgdef\multilingual\MultilingualBehavior;
public function behaviors()
{
    return [
         'ml' => [
            'class' => MultilingualBehavior::className(),
            'languages' => [
                'vi-VN' => 'Russian',
                'en-US' => 'English',
            ],
            //'languageField' => 'language',
            //'localizedPrefix' => '',
            //'requireTranslations' => false',
            //'dynamicLangClass' => true',
            'langClassName' => PostLang::className(), // or namespace/for/a/class/PostLang
            'defaultLanguage' => 'vi-VN',
            'langForeignKey' => 'post_id',
            'tableName' => "{{%postLang}}",
            'attributes' => [
                'title', 'content',
            ]
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}

和输出错误

调用未定义的方法 backend\controllers\PostlangController::primaryKey()

4

1 回答 1

1

不要将行为添加到控制器中,将其添加到模型中

将此行为附加到模型(示例中的帖子)。

https://github.com/OmgDef/yii2-multilingual-behavior

于 2015-10-08T07:38:44.430 回答