Yii2多语种。无法从转换表到达字段

时间:2018-03-10 16:10:29

标签: yii2 yii2-advanced-app multilingual

我通过composer安装了OmgDef / Multilingual。从指南一步一步完成了所有事情。我现在得到的错误是:

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

我添加了joinWith('translation'),但没有任何变化。这是我的PageAdmin模型和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;
    }
}

languageDefaultbg。有人有同样的问题吗?解释不是很大,但我认为问题很明显:)感谢每一条建议!

1 个答案:

答案 0 :(得分:1)

没有使用它,但是查看代码,您在模型中的joinWith函数中添加了search(),您是否在title内搜索translations内的任何字段{1}}表使用某些gridview或搜索表单

如果是这样,您需要在searchModel中声明自定义attribute并将其添加到安全rules,然后使用它,因为您在该行收到错误

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

所以在PageAdminSearch

之上添加自定义属性
public $title

使用关系的别名

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

然后将您的规则更新为以下

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

并将该行更改为以下

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

现在运行它不会显示错误。