0

我正在尝试将多语言行为集成到我的 Yii 演示项目“Trackstar”中。模型“项目”设置用于测试这种多语言行为。

当我尝试创建一个新项目时,我收到一个 PHP 致命错误:最大执行时间超过 30 秒(几个不同的 php 脚本)。

调试此错误,出现奇怪的 foreach 行为/models/behaviors/MultilingualBehavior.php line 298:

foreach ($this->languages as $l) { //$this->languages=array('en','lt');

var_dump($l) outputs very long list of 
s`tring(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt" string(2) "en" string(2) "lt"`

我正在使用 MAMP,php 版本 5.4.10

更多信息:

model/Project.php 中的行为方法

public function behaviors() {
    return array(
        'ml' => array(
            'class' => 'application.models.behaviours.MultilingualBehavior',
            'langClassName' => 'Project',
            'langTableName' => 'tbl_projectLang',
            'langForeignKey' => 'project_id',
            'langField' => 'lang_id',
            'localizedAttributes' => array('name', 'description'), //attributes of the model to be translated
            'localizedPrefix' => 'l_',
            'languages' => array('en','lt'),
            'defaultLanguage' => 'en',
            'createScenario' => 'insert',
            //'localizedRelation' => 'i18nPost',
            'multilangRelation' => 'multilangProject',
            'forceOverwrite' => true,
            //'forceDelete' => true, 
            //'dynamicLangClass' => true, //Set to true if you don't want to create a 'PostLang.php' in your models folder
        ),
    );
}

/models/behaviours/MultilingualBehavior.php 方法附:

public function attach($owner) {
        parent::attach($owner);
        $owner_classname = get_class($owner);
        $table_name_chunks = explode('.', $owner->tableName());
        $simple_table_name = str_replace(array('{{', '}}'), '', array_pop($table_name_chunks));

        if (!isset($this->langClassName)) {
            $this->langClassName = $owner_classname . 'Lang';
        }
        if (!isset($this->langTableName)) {
            $this->langTableName = $simple_table_name . 'Lang';
        }
        if (!isset($this->localizedRelation)) {
            $this->localizedRelation = 'i18n' . $owner_classname;
        }
        if (!isset($this->multilangRelation)) {
            $this->multilangRelation = 'multilang' . $owner_classname;
        }
        if (!isset($this->langForeignKey)) {
            $this->langForeignKey = $simple_table_name . '_id';
        }
        if ($this->dynamicLangClass) {
            $this->createLangClass();
        }
        if (array_values($this->languages) !== $this->languages) { //associative array
            $this->languages = array_keys($this->languages);
        }
        $class = CActiveRecord::HAS_MANY;
        $this->createLocalizedRelation($owner, Yii::app()->language);
        $owner->getMetaData()->relations[$this->multilangRelation] = new $class($this->multilangRelation, $this->langClassName, $this->langForeignKey, array('index' => $this->langField));
        $rules = $owner->rules();
        $validators = $owner->getValidatorList();
        foreach ($this->languages as $l) {
            foreach($this->localizedAttributes as &$attr) {
                foreach($rules as $rule) {
                    $rule_attributes = array_map('trim', explode(',', $rule[0]));
                    if(in_array($attr, $rule_attributes)) {
                        if ($rule[1] !== 'required' || $this->forceOverwrite) {
                            $validators->add(CValidator::createValidator($rule[1], $owner, $attr . '_' . $l, array_slice($rule, 2)));
                        }
                        else if($rule[1] === 'required') {
                            //We add a safe rule in case the attribute has only a 'required' validation rule assigned
                            //and forceOverWrite == false
                            $validators->add(CValidator::createValidator('safe', $owner, $attr . '_' . $l, array_slice($rule, 2)));
                        }
                    }
                }
            } 
        } 
    }

自己解决了。我的错误是

'langClassName' => 'Project',在 /models/Project.php

这使得项目类实例的递归调用+多语言行为类女巫调用项目类等等。将 'langClassName' => 'Project' 更改为 'langClassName' => 'ProjectLang',出现错误 在数据库中找不到活动记录类“ProjectLang”的表“{{tbl_projectLang}}”。将 'tablePrefix' => 'tbl_' 添加到 /config/main.php db 组件,一切似乎都很好。

4

0 回答 0