我有一个包含字段(id,name)的agents表
我有一个包含字段(agent_id,aggregator_id)的agent_aggregators表
我有一个带有字段的transactions表(agent_id)
Aggregators表通过agent_id连接到agents表。
Transactions表有一个agent_id字段。
现在,在我的transactions视图中,我有一个index.php,我想要显示执行特定事务的聚合器的名称
事务视图/ index.php
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'agent.aggregators.name',
label' => 'Aggregator\'s Name',
]交易模型:
<?php
namespace app\models;
use Yii;
class Transactions extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'transactions';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'ID' => 'ID',
];
}
public function getAgent()
{
return $this->hasOne(Agents::className(), ['ID' => 'agent_id']);
}
}聚合器模型:
<?php
namespace app\models;
use Yii;
class AgentsAggregators extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'agents_aggregators';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'agent_id' => 'Agent ID',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAgent()
{
return $this->hasOne(Agents::className(), ['ID' => 'agent_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAggregator()
{
return $this->hasOne(Agents::className(), ['ID' => 'aggregator_id']);
}
}代理模型:
<?php
namespace app\models;
use Yii;
class Agents extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'agents';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'ID' => 'ID',
'name' => 'Agent\'s Name',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAgentsAggregators()
{
return $this->hasMany(AgentsAggregators::className(), ['agent_id' => 'ID']);
}
public function getAggregators()
{
return $this->hasMany(Agents::className(), ['ID' => 'aggregator_id'])->viaTable('agents_aggregators', ['agent_id' => 'ID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAgents()
{
return $this->hasMany(Agents::className(), ['ID' => 'agent_id'])->viaTable('agents_aggregators', ['aggregator_id' => 'ID']);
}
}发布于 2019-03-07 00:02:07
事务处理表和座席表之间的关系是一对多关系。然而,代理对聚合器是多对多的。因此,您不能获得单个聚合器(严格意义上),但它将始终是聚合器的集合(尽管可能只有一个元素)。这段代码应该说明你正在尝试做什么。
如果我误解了你的问题,请在评论中告诉我。
[
'label' => "Aggregator's Name",
'format' =>'ntext',
'value' => function($model)
{
$aggrs = $model->agent->aggregators;
return implode('\n', $aggrs);
}
]https://stackoverflow.com/questions/54924130
复制相似问题