Yii2 多语言。无法访问翻译中的字段 table
Yii2 Multilingual. Can't reach the fields from the translation table
我通过 Composer 安装了 OmgDef/Multilingual。从指南中一步步完成所有操作。我现在得到的错误是:
Getting unknown property: backend\models\PageAdminSearch::title
我添加了 joinWith('translation')
,但没有 changes.This 是我的 PageAdmin
模型和 PageAdminSearch
页面管理员:
<?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
是 bg
。有人有同样的问题吗?解释不是很大,但我认为问题很清楚:)感谢每一个建议!
虽然没用过,但看看你的代码,你在模型的 search()
函数中添加了一个 joinWith
,你是否在里面搜索名称为 title
的任何字段translations
table 使用一些网格视图或搜索表单
如果是这样,您需要在 searchModel 中声明一个自定义 attribute
并将其添加到安全 rules
中,然后使用它,因为您在行 [=22] 处遇到错误=]
$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]);
现在 运行 它不会向您显示错误。
我通过 Composer 安装了 OmgDef/Multilingual。从指南中一步步完成所有操作。我现在得到的错误是:
Getting unknown property: backend\models\PageAdminSearch::title
我添加了 joinWith('translation')
,但没有 changes.This 是我的 PageAdmin
模型和 PageAdminSearch
页面管理员:
<?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
是 bg
。有人有同样的问题吗?解释不是很大,但我认为问题很清楚:)感谢每一个建议!
虽然没用过,但看看你的代码,你在模型的 search()
函数中添加了一个 joinWith
,你是否在里面搜索名称为 title
的任何字段translations
table 使用一些网格视图或搜索表单
如果是这样,您需要在 searchModel 中声明一个自定义 attribute
并将其添加到安全 rules
中,然后使用它,因为您在行 [=22] 处遇到错误=]
$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]);
现在 运行 它不会向您显示错误。