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?