Php 找不到变体
我想用这个为ActiveRecord类FaqCategory实现i18n特性。 这是我的桌子:Php 找不到变体,php,yii2,yii-extensions,Php,Yii2,Yii Extensions,我想用这个为ActiveRecord类FaqCategory实现i18n特性。 这是我的桌子: CREATE TABLE `FaqCategory` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `icon` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_
CREATE TABLE `FaqCategory`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
CREATE TABLE
`FaqCategoryTranslation`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`catID` int(10) unsigned DEFAULT NULL,
`languageID` tinyint(2) unsigned NOT NULL,
`title` varchar(255) DEFAULT NULL,
`slug` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `catID` (`catID`) USING BTREE,
KEY `languageID` (`languageID`),
CONSTRAINT `FCT_FK1` FOREIGN KEY (`catID`) REFERENCES `FaqCategory` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FCT_FK2` FOREIGN KEY (`languageID`) REFERENCES `Language` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
CREATE TABLE `Language`
(
`id` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
`locale` varchar(2) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
常见问题解答类别模型:
class FaqCategory extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'translationBehavior' => [
'class' => VariationBehavior::className(),
'variationsRelation' => 'translations',
'defaultVariationRelation' => 'defaultTranslation',
'variationOptionReferenceAttribute' => 'languageID',
'optionModelClass' => Language::className(),
'defaultVariationOptionReference' => function () {
return Yii::$app->language;
},
'variationAttributeDefaultValueMap' => [
'title' => 'name'
],
],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'FaqCategory';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['icon', 'name'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'icon' => 'Icon',
];
}
public static function find()
{
return parent::find()->with('defaultTranslation');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTranslations()
{
return $this->hasMany(FaqCategoryTranslation::className(), ['catID' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getDefaultTranslation()
{
return $this->hasDefaultVariationRelation(); // convert "has many translations" into "has one defaultTranslation"
}
/**
* @return \yii\db\ActiveQuery
*/
public function getFaqs()
{
return $this->hasMany(Faq::className(), ['catID' => 'id']);
}
创建操作:
public function actionCreate()
{
$model = new FaqCategory();
$post = Yii::$app->request->post();
if ($model->load($post) && Model::loadMultiple($model->getVariationModels(), $post) && $model->save()) {
return $this->redirect(['index']);
} else
return $this->render('create', [
'model' => $model,
]);
}
和查看文件:
<div class="faq-category-create">
<h1><?= Html::encode($this->title) ?></h1>
<div class="faq-category-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name'); ?>
<? foreach ($model->getVariationModels() as $index => $variationModel): ?>
<?= $form->field($variationModel, "[{$index}]title")->label($variationModel->getAttributeLabel('title') . ' (' . $variationModel->languageID . ')'); ?>
<?php endforeach; ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
查看actionIndex的文件:
<div class="cat-descr"><?= $category->title ?></div>
一切正常,所有数据都正确地插入到带有languageID的FaqCategoryTranslation表中。但当我想从翻译表中获取这些值时,它只返回FaqCategory表中name字段的值。最后,我不完全理解AttributedFaultValueMap。此属性用于什么?换句话说,如果我们的默认应用程序语言是例如en,为什么不使用en title作为默认语言。
谢谢 我想你应该提到: 例如:
$items = Item::find()->with('translations')->all(); // only 2 queries will be performed
foreach ($items as $item) {
echo $item->title . '<br>'; // add other stuffs you want to produce, like name, id or whatever you may have.
var_dump($item->defaultTranslation); // no extra query, `defaultTranslation` is populated from `translations`
}
$items=Item::find()->with('translations')->all();//将只执行2个查询
foreach($items作为$item){
echo$item->title.“
”;//添加您想要生成的其他内容,例如名称、id或您可能拥有的任何内容。
var_dump($item->defaultTranslation);//没有额外的查询,`defaultTranslation`由`translations'填充`
}
尝试打印($categories)。看看有什么回报,如果你能在这里分享,我会很高兴的。:)
$items = Item::find()->with('translations')->all(); // only 2 queries will be performed
foreach ($items as $item) {
echo $item->title . '<br>'; // add other stuffs you want to produce, like name, id or whatever you may have.
var_dump($item->defaultTranslation); // no extra query, `defaultTranslation` is populated from `translations`
}