I have the following 3 tables:
Rule
-id
-name
CombinedRule
-id
-name
RuleCombineMapping
-id_rule
-id_combine
I generated a CRUD for Rule and for the CombinedRule table. Inside the CombinedRule model class I created a mapping the class looks like the following:
<?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']);
}
}
Without success I tried to access the rules of a certain CombinedRule by using the following lines inside the CombinedRuleController
.
$t = CombinedRule::find($id);
var_dump($t->rules);
The results is always a 'Unknown Property' exception.
Now I want to view/update/read/delete not only Rules and CombinedRules but also the relation between those two.
I know this is possible in other frameworks using doctrine and I also know how to do it manually first fetching the relation and then adding it to a list.
Now does anybody has a working example how to map this tables using a similar established data structure and also integrate it as easy as possible in the Gii CRUD using its front-end models, views and forms?
I've tried it now myself and it worked for me. That is, var_dump($model->rules);
in view file gave me an array with Rule objects as expected.
Here are my gii generated files. I have removed comments, attributeLabels(), rules() methods from model classes and action methods and behaviours() from the controller class. So this is the essential code that is required to make $model->rules working:
Rule
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 has also generated two methods getIdCombine()
and getIdRule()
which are also not essential for the issue.
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.');
}
}
}
views/combined-rule/view.php
Just added var_dump($model->rules);
. Other is gii generated code.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.