Note that there are some explanatory texts on larger screens.

plurals
  1. POphp foreach problems in Yii framework
    primarykey
    data
    text
    <p>I'm trying to integrate Multilingual behaviour into my Yii demo project "Trackstar". Model "project" is set for testing this multilingual behavior. </p> <p>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).</p> <p>Debugging this eror, got strange foreach behavior in <code>/models/behaviors/MultilingualBehavior.php line 298:</code></p> <pre><code>foreach ($this-&gt;languages as $l) { //$this-&gt;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"` </code></pre> <p>I'm using MAMP, php version 5.4.10</p> <p>more info:</p> <p>behaviors method in model/Project.php</p> <pre><code>public function behaviors() { return array( 'ml' =&gt; array( 'class' =&gt; 'application.models.behaviours.MultilingualBehavior', 'langClassName' =&gt; 'Project', 'langTableName' =&gt; 'tbl_projectLang', 'langForeignKey' =&gt; 'project_id', 'langField' =&gt; 'lang_id', 'localizedAttributes' =&gt; array('name', 'description'), //attributes of the model to be translated 'localizedPrefix' =&gt; 'l_', 'languages' =&gt; array('en','lt'), 'defaultLanguage' =&gt; 'en', 'createScenario' =&gt; 'insert', //'localizedRelation' =&gt; 'i18nPost', 'multilangRelation' =&gt; 'multilangProject', 'forceOverwrite' =&gt; true, //'forceDelete' =&gt; true, //'dynamicLangClass' =&gt; true, //Set to true if you don't want to create a 'PostLang.php' in your models folder ), ); } </code></pre> <p>/models/behaviours/MultilingualBehavior.php method attach:</p> <pre><code>public function attach($owner) { parent::attach($owner); $owner_classname = get_class($owner); $table_name_chunks = explode('.', $owner-&gt;tableName()); $simple_table_name = str_replace(array('{{', '}}'), '', array_pop($table_name_chunks)); if (!isset($this-&gt;langClassName)) { $this-&gt;langClassName = $owner_classname . 'Lang'; } if (!isset($this-&gt;langTableName)) { $this-&gt;langTableName = $simple_table_name . 'Lang'; } if (!isset($this-&gt;localizedRelation)) { $this-&gt;localizedRelation = 'i18n' . $owner_classname; } if (!isset($this-&gt;multilangRelation)) { $this-&gt;multilangRelation = 'multilang' . $owner_classname; } if (!isset($this-&gt;langForeignKey)) { $this-&gt;langForeignKey = $simple_table_name . '_id'; } if ($this-&gt;dynamicLangClass) { $this-&gt;createLangClass(); } if (array_values($this-&gt;languages) !== $this-&gt;languages) { //associative array $this-&gt;languages = array_keys($this-&gt;languages); } $class = CActiveRecord::HAS_MANY; $this-&gt;createLocalizedRelation($owner, Yii::app()-&gt;language); $owner-&gt;getMetaData()-&gt;relations[$this-&gt;multilangRelation] = new $class($this-&gt;multilangRelation, $this-&gt;langClassName, $this-&gt;langForeignKey, array('index' =&gt; $this-&gt;langField)); $rules = $owner-&gt;rules(); $validators = $owner-&gt;getValidatorList(); foreach ($this-&gt;languages as $l) { foreach($this-&gt;localizedAttributes as &amp;$attr) { foreach($rules as $rule) { $rule_attributes = array_map('trim', explode(',', $rule[0])); if(in_array($attr, $rule_attributes)) { if ($rule[1] !== 'required' || $this-&gt;forceOverwrite) { $validators-&gt;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-&gt;add(CValidator::createValidator('safe', $owner, $attr . '_' . $l, array_slice($rule, 2))); } } } } } } </code></pre> <p>Solved it myself. My mistake was</p> <blockquote> <p><code>'langClassName' =&gt; 'Project',</code> in /models/Project.php</p> </blockquote> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload