2015-11-19 11 views
10

मैं इस extension का उपयोग कर 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_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 

FaqCategory मॉडल: यहाँ मेरी टेबल हैं

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> 

दृश्यपटल में:

public function actionIndex() 
    { 
     $categories = FaqCategory::find()->with('translations')->all(); 
     return $this->render('index', ["categories" => $categories]); 

    } 

actionIndex की फ़ाइल दृश्य:

<div class="cat-descr"><?= $category->title ?></div> 

सब कुछ ठीक था, सभी डेटा LanguageID साथ FaqCategoryTranslation मेज पर ठीक से insterted। लेकिन जब मैं इन मानों को अनुवाद तालिका से प्राप्त करना चाहता हूं, तो यह केवल FaqCategory तालिका में नाम फ़ील्ड का मान देता है। अंत में, मुझे पूरी तरह से विशेषता नहीं है DaultaultValueMap। इस विशेषता का क्या उपयोग किया जाता है? दूसरे शब्दों में, यदि हमारी डिफ़ॉल्ट ऐप भाषा उदाहरण के लिए en क्यों है en डिफ़ॉल्ट रूप से शीर्षक का उपयोग न करें। धन्यवाद!

उत्तर

1

मुझे लगता है कि आप इस संदर्भ दिया जाना चाहिए: https://github.com/yii2tech/ar-variation

उदाहरण के लिए:

$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` 
} 
+0

print_r ($ श्रेणियों) करके देखें। देखें कि यह क्या लौटाता है और अगर आप इसे यहां साझा कर सकते हैं तो मुझे खुशी होगी। :) –

संबंधित मुद्दे