Toma Tomov
Toma Tomov

Reputation: 1694

Yii2 Multilingual. Can't reach the fields from the translation table

I installed OmgDef/Multilingual via composer. Did everything step by step from the guide. The error that I get now is:

Getting unknown property: backend\models\PageAdminSearch::title

I added joinWith('translation') but nothing changes.This is my PageAdmin model and PageAdminSearch PageAdmin:

<?php

namespace backend\models;

use omgdef\multilingual\MultilingualBehavior;
use omgdef\multilingual\MultilingualQuery;
use Yii;

/**
 * This is the model class for table "page_admin".
 *
 * @property int $id
 * @property int $id_in
 * @property int $enable
 * @property string $icon
 */
class PageAdmin extends \yii\db\ActiveRecord
{
    public static function find()
    {
        return new MultilingualQuery(get_called_class());
    }

    public function behaviors()
    {
        $allLanguages = [];
        foreach (Yii::$app->params['languages'] as $title => $language) {
            $allLanguages[$title] = $language;
        }

        return [
            'ml' => [
                'class' => MultilingualBehavior::className(),
                'languages' => $allLanguages,
                //'languageField' => 'language',
                //'localizedPrefix' => '',
                //'requireTranslations' => false',
                //'dynamicLangClass' => true',
                //'langClassName' => PostLang::className(), // or namespace/for/a/class/PostLang
                'defaultLanguage' => Yii::$app->params['languageDefault'],
                'langForeignKey' => 'page_id',
                'tableName' => "{{%page_adminlang}}",
                'attributes' => [
                    'title',
                    'content',
                ]
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'page_admin';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['icon'], 'string'],
            [['id_in', 'enable'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'id_in' => Yii::t('app', 'Id In'),
            'icon' => Yii::t('app', 'Icon'),
        ];
    }
}

PageAdminSearch:

<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\PageAdmin;

/**
 * PageAdminSearch represents the model behind the search form of `backend\models\PageAdmin`.
 */
class PageAdminSearch extends PageAdmin
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'id_in'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PageAdmin::find()->joinWith('translations');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'id_in' => $this->id_in,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title]);

        return $dataProvider;
    }
}

languageDefault is bg. Did someone have the same problem? The explanation is not pretty big but I think the problem is clear enough :) Appreciate every advice!

Upvotes: 1

Views: 505

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23778

Haven't used it tho but looking at your code you are adding a joinWith inside the search() function in the model, are you searching any field with name title inside the translations table using some gridview or search form

If that is so you need to declare a custom attribute inside your searchModel and add it to the safe rules and then use it because you are getting an error at the line

$query->andFilterWhere(['like', 'title', $this->title]);

so add a custom attribute on top of your PageAdminSearch

public $title

And it is always good to use an alias for the relation

 $query = PageAdmin::find()
 ->joinWith(['translations'=>function($q){
    $q->from('{{%transalations}} tr');
 }]);

then update your rules to the following

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'id_in'], 'integer'],
        [['title'],'safe'],
    ];
}

and change the line to the following

$query->andFilterWhere(['like', 'tr.title', $this->title]);

Now run it won't show you the error.

Upvotes: 1

Related Questions