I'm trying to integrate Multilingual behaviour into my Yii demo project "Trackstar". Model "project" is set for testing this multilingual behavior.
When I try to create an new project, I got an PHP Fatal error: Maximum execution time of 30 seconds exceeded in (several different php scripts).
Debugging this eror, got strange foreach behavior in /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"`
I'm using MAMP, php version 5.4.10
more info:
behaviors method in 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 method attach:
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)));
}
}
}
}
}
}
Solved it myself. My mistake was
'langClassName' => 'Project',
in /models/Project.php
that made recursive call of Project class instance + multilingualbehavior class witch call Project class an so on. Changed 'langClassName' => 'Project', to 'langClassName' => 'ProjectLang', got error The table "{{tbl_projectLang}}" for active record class "ProjectLang" cannot be found in the database. added 'tablePrefix' => 'tbl_', to /config/main.php db component and everything seems to be ok.