Yii 2.0视图,适用于带有外键的型号
我有三张桌子: 第一个表是Sims4页的主表。它具有名为id的主键。 第二个表是关系表Yii 2.0视图,适用于带有外键的型号,yii,yii2,foreign-keys,relational-database,foreign-key-relationship,Yii,Yii2,Foreign Keys,Relational Database,Foreign Key Relationship,我有三张桌子: 第一个表是Sims4页的主表。它具有名为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 `TheSims4P
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`);
第三个表是Sims4PagesCategories,带有PK id
目前我有4个模拟模型页面:
<?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']);
}
}
但是关系表呢
也就是说,我想使用Sims4PagesCategories表中的所有类别创建选择项,并将多个类别选择保存到模型中
$model = new TheSims4Pages();
多谢各位 视图中:其中“categoryName”是类别表中用作标签的列
<?= $form->field($model, 'categories')
->dropDownList(ArrayHelper::map(TheSims4PagesCategories::find()->all(),
'id',
'categoryName'
), ['multiple'=>'multiple']) ?>
这并不能最好地解决数据库完整性问题,但它可以完成这项工作,否则,您可能需要使用TransactionHi!非常感谢你!但是,如果我的表有FKs,为什么这么复杂?例如,在thymeleaf.org Java模板引擎中,对于许多字段的值,我可以编写类似Covered的内容,它将使用lazy方法访问Sims4PageScateGories关系。Yii 2.0也有类似的方法吗?好吧,这就是它在Yii中的工作方式,关系是只读属性,而不是像属性一样我停留在某一时刻。如何在Sims4Pages模型中声明@property categories?此示例显示获取未知属性时出错:app\models\TheSims4\TheSims4Pages::categories
<?= $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();
}
}
}
}