我有以下3个表:
Rule
-id
-name
CombinedRule
-id
-name
RuleCombineMapping
-id_rule
-id_combine
我为Rule和CombinedRule表生成了一个CRUD。在CombinedRule模型类中,我创建了一个映射,该类如下所示:
<?php
namespace app'models;
use Yii;
/**
* This is the model class for table "combinedrule".
*
* @property integer $id
* @property string $name
*/
class CombinedRule extends 'yii'db'ActiveRecord {
/**
* @inheritdoc
*/
public static function tableName() {
return 'combinedrule';
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['name'], 'string', 'max' => 255],
[['name'], 'unique']
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'name' => 'Name',
];
}
public function getRules() {
return $this->hasMany(Rule::className(), ['id' => 'id_rule'])
->viaTable(RuleCombineMapping::tableName(), ['id_combine' => 'id']);
}
}
没有成功,我试图通过使用CombinedRuleController
内的以下行访问某个CombinedRule的规则。
$t = CombinedRule::find($id);
var_dump($t->rules);
结果总是一个'Unknown Property'异常。
现在我想查看/更新/读取/删除不仅Rules和combinerules,还有它们之间的关系。
我知道这是可能的在其他框架使用原则,我也知道如何做到这一点手动首先获取关系,然后将其添加到一个列表。
现在有没有人有一个工作的例子,如何使用类似的数据结构来映射这些表,并使用其前端模型,视图和表单尽可能容易地将其集成到Gii CRUD中?
我已经试过了,它对我很有效。也就是说,var_dump($model->rules);
在视图文件中给了我一个具有预期规则对象的数组。
class Rule extends 'yii'db'ActiveRecord {
public static function tableName() {
return 'rule';
}
}
CombinedRule
class CombinedRule extends 'yii'db'ActiveRecord {
public static function tableName() {
return 'combined_rule';
}
// Added this manually, this does not come from gii!
// It is the single code that I've added.
public function getRules() {
return $this->hasMany(Rule::className(), ['id' => 'id_rule'])
->viaTable(RuleCombineMapping::tableName(), ['id_combine' => 'id']);
}
}
RuleCombineMapping
Gii还生成了getIdCombine()
和getIdRule()
两种方法,这两种方法对于问题也不是必需的。
class RuleCombineMapping extends 'yii'db'ActiveRecord {
public static function tableName() {
return 'rule_combine_mapping';
}
}
CombinedRuleController
class CombinedRuleController extends Controller {
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
protected function findModel($id) {
if (($model = CombinedRule::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
视图/combined-rule view.php
刚刚添加了var_dump($model->rules);
。其他是gii生成的代码。
use yii'helpers'Html;
use yii'widgets'DetailView;
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Combined Rules', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="combined-rule-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => ['id', 'title'],
]) ?>
<?php
// And here it is: an array of Rule objects!!!!!
var_dump($model->rules);
?>
</div>