0
私は3つのテーブルを持っています:外部キー付きモデルのYii 2.0ビュー
最初のテーブルはメインテーブルTheSims4Pages
です。それにはid
という名前の主キーがあります。 2番目の表は、関連テーブル
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`);
され、第3表は、PK id
と、TheSims4PagesCategories
あります。
現在、私は、モデル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']);
}
}
モデルのgetTheSims4PagesCategoriesRelations
、
echo $form->field($model, 'categoryId')->dropDownList($listData);
のようなもののためではなく、関係テーブルのselect要素で、TheSims4Pages
のためのビューを作成する方法は?
Ieが、私はTheSims4PagesCategoriesテーブルからすべてのカテゴリを選択する項目を作成し、モデル
$model = new TheSims4Pages();
へのカテゴリの複数選択を保存するには、どうもありがとうございました!ビューで
これは、DBの整合性の問題、それを最善を解決しませんすることができますが、それは仕事をするでしょう、そうでなければ、あなたはトランザクションを使用したいかもしれません – Ripper
こんにちは!どうもありがとうございました!しかし、私のテーブルにFKがある場合、なぜそんなに複雑なのですか?例えば、多くのフィールド値のためのthymeleaf.org Javaテンプレートエンジンでは、 '
まあ、これはYiiで動作する方法です、リレーションは属性のように読み込まれません。 – Ripper