Question

I have followed Yii extension: Multilingual-behavior instructions typically, and here is my model behavior config:

public function behaviors() 
{
    return array(
        'ml' => array(
            'class' => 'application.models.behaviors.MultilingualBehavior',
            'langClassName' => 'CandidateLang',
            'langTableName' => 'candidatelang',
            'langForeignKey' => 'candidate_id',
            'langField' => 'lang_id',
            'localizedAttributes' => array('name', 'birth_place', 'home_city', 'intro'), //attributes of the model to be translated
            'localizedPrefix' => 'l_',
            'languages' => Yii::app()->params['languages'], // array of your translated languages. Example : array('fr' => 'Français', 'en' => 'English')
            'defaultLanguage' => 'en', //your main language. Example : 'fr'
            'createScenario' => 'insert',
            'localizedRelation' => 'i18nCandidate',
            'multilangRelation' => 'multilangCandidate',
            'forceOverwrite' => false,
            'forceDelete' => true, 
            'dynamicLangClass' => true, //Set to true if you don't want to create a 'PostLang.php' in your models folder
        ),
    );
}

I get a CDbException: The table "{{candidatelang}}" for active record class "CandidateLang" cannot be found in the database.

Here is where the error triggered:

private $_model;
2253 
2254     /**
2255      * Constructor.
2256      * @param CActiveRecord $model the model instance
2257      */
2258     public function __construct($model)
2259     {
2260         $this->_model=$model;
2261 
2262         $tableName=$model->tableName();
2263         if(($table=$model->getDbConnection()->getSchema()-   >getTable($tableName))===null)
2264             throw new CDbException(Yii::t('yii','The table "{table}" for active record class "{class}" cannot be found in the database.',
2265                 array('{class}'=>get_class($model),'{table}'=>$tableName)));
2266         if($table->primaryKey===null)
2267         {
2268             $table->primaryKey=$model->primaryKey();
2269             if(is_string($table->primaryKey) && isset($table->columns[$table->primaryKey]))
2270                 $table->columns[$table->primaryKey]->isPrimaryKey=true;
2271             else if(is_array($table->primaryKey))
2272             {
2273                 foreach($table->primaryKey as $name)
2274                 {
2275                     if(isset($table->columns[$name]))
2276                         $table->columns[$name]->isPrimaryKey=true;

I have made sure the table is there and double checked the casing and the spelling. I even tried putting different table names from the schema without any luck!! So my guess that this call is causing the error: $model->getDbConnection() , but i don't know Why or How to fix it??

Was it helpful?

Solution

I had the same problem and i fixed it. In the main config set tablePrefix => '' and the problem should be fixed.

OTHER TIPS

I have tried to force the auto generated model db variable like this:

    public function createLangClass() {
    if(!class_exists($this->langClassName, false)) {
        $owner_classname = get_class($this->getOwner());
        eval("class {$this->langClassName} extends CActiveRecord
        {
                  //Forcing model db variable
            self::db = Yii::app()->getDb();

            public static function model(\$className=__CLASS__)
            {
                return parent::model(\$className);
            }

            public function tableName()
            {
                return '{{{$this->langTableName}}}';
            }

            public function relations()
            {
                return array('$owner_classname' => array(self::BELONGS_TO, '$owner_classname', '{$this->langForeignKey}'));
            }

        }");
    }
} 

I still don't know why!! but it seems to work, since the CdbException is gone.

However i got a new error, Yiibase complaining about not finding the dynamic auto generated model file!! I had no experience with dynamic classes in Yii before but i still suspect this issue is new with yii1.1.10. If you have any quick fixes plz comment, otherwise i will post a new question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top