0

我有3個表:的Yii 2.0視圖模型與外鍵

第一個表是主表TheSims4Pages。它具有名爲id的主鍵。 第二個表是關係表

CREATE TABLE IF NOT EXISTS `TheSims4PagesCategoriesRelations` (
    `id` int(11) NOT NULL, 
    `postId` int(11) NOT NULL, 
    `catId` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

ALTER TABLE `TheSims4PagesCategoriesRelations` 
    ADD CONSTRAINT `TheSims4PagesCategoriesRelations_ibfk_2` FOREIGN KEY (`catId`) REFERENCES `TheSims4PagesCategories` (`id`), 
    ADD CONSTRAINT `TheSims4PagesCategoriesRelations_ibfk_1` FOREIGN KEY (`postId`) REFERENCES `TheSims4Pages` (`id`); 

;第三表TheSims4PagesCategories,與PK id

目前,我有模型TheSims4Pages:

<?php 

namespace app\models\TheSims4; 

use Yii; 

/** 
* This is the model class for table "TheSims4Pages". 
* 
* @property integer $id 
* @property string $pageName 
* @property string $pageEditDate 
* @property integer $isDraft 
* @property integer $authorId 
* @property string $pageText 
* 
* @property SiteUsers $author 
* @property TheSims4PagesCategoriesRelations[] $theSims4PagesCategoriesRelations 
*/ 
class TheSims4Pages extends \yii\db\ActiveRecord { 

    /** 
    * @inheritdoc 
    */ 
    public static function tableName() { 
     return 'TheSims4Pages'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() { 
     return [ 
      [['pageName', 'authorId', 'pageText'], 'required'], 
      [['pageEditDate'], 'safe'], 
      [['isDraft', 'authorId'], 'integer'], 
      [['pageText'], 'string'], 
      [['pageName'], 'string', 'max' => 500], 
      [['authorId'], 'exist', 'skipOnError' => true, 'targetClass' => SiteUsers::className(), 'targetAttribute' => ['authorId' => 'id']], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() { 
     return [ 
      'id' => 'ID', 
      'pageName' => 'Page Name', 
      'pageEditDate' => 'Page Edit Date', 
      'isDraft' => 'Is Draft', 
      'authorId' => 'Author ID', 
      'pageText' => 'Page Text', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getAuthor() { 
     return $this->hasOne(SiteUsers::className(), ['id' => 'authorId']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getTheSims4PagesCategoriesRelations() { 
     return $this->hasMany(TheSims4PagesCategoriesRelations::className(), ['postId' => 'id']); 
    } 

} 

如何使視圖TheSims4Pages,與選擇元素的模型getTheSims4PagesCategoriesRelations,像

echo $form->field($model, 'categoryId')->dropDownList($listData); 

但對於關係表?

也就是說,我想使選擇項與TheSims4PagesCategories表中的所有類別和保存類別的多重選擇到模型

$model = new TheSims4Pages(); 

非常感謝您!

回答

1

鑑於:(其中「類別名稱」是用作標籤從類別表列)

<?= $form->field($model, 'categories') 
    ->dropDownList(ArrayHelper::map(TheSims4PagesCategories::find()->all(), 
     'id', 
     'categoryName' 
    ), ['multiple'=>'multiple']) ?> 

在模型添加虛擬變量

// ... 

class TheSims4Pages extends \yii\db\ActiveRecord { 

    public $categories; 

    public function rules() 
    { 
     return [ 
      ['categories', 'each', 'rule' => ['integer']], 
      // your other rules 
     ]; 
    } 

    // ... 


    public function afterFind() 
    { 
     parent::afterFind(); 

     $this->categories = []; 

     if (!empty($this->theSims4PagesCategoriesRelations)) { 
      foreach ($this->theSims4PagesCategoriesRelations as $cat) { 
       $categories[$cat->catId] = $cat->catId; 
      } 
      $this->categories = $categories; 
     } 
    } 

    /* deleting might work better in afterSave 
     due to ending up with posts without categories on constraint fail 
    public function beforeSave($insert) 
    { 
     parent::beforeSave($insert); 

     if ($insert) { 
     } else { 
      TheSims4PagesCategoriesRelations::deleteAll(['postId' => $this->id]); 
     } 
    } 
    */ 

    public function afterSave($insert, $changedAttributes) 
    { 
     parent::afterSave($insert, $changedAttributes); 

     if ($insert) { 
     } else { 
      TheSims4PagesCategoriesRelations::deleteAll(['postId' => $this->id]); 
     } 

     if ($this->categories) { 
      foreach ($this->categories as $cat_id) { 
       $relation = new TheSims4PagesCategoriesRelations(); 
       $relation->postId = $this->id; 
       $relation->catId = $cat_id; 
       $relation->save(); 
      } 
     } 
    } 
} 
+0

不能解決分貝完整性問題最好它可以,但它會做的工作,否則,你可能想使用交易 – Ripper

+0

嗨!非常感謝你!但爲什麼如此複雜,如果我的桌子有FK呢?例如,在thymeleaf.org的Java模板引擎中,我可以編寫類似於'

'(http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#checkbox-fields)的東西將使用懶惰的方法訪問SnS4PagesCategoriesRelations。 Yii 2.0有類似的方法嗎? –

+0

好吧,這是Yii的工作原理,關係是隻讀屬性,不像屬性 – Ripper