我有三张桌子。Product [ProductId, ProductName],VendorProduct[VPid, ProductId, VPprice],Deals[DealId, VPid, DealPrice]
我想在GridView中显示使用活动记录的交易。我定义了关系,但无法从表产品访问产品名称。Deals.*, VendorProduct.VPid, Product.ProductName
这就是我想要的结果。
我的交易模式是这样的。
$query = Deals::find()->joinWith(['vendorProduct', 'vendorProduct.product']);
我就这样把它传给。
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
我的GridView代码:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'DealId',
'VPid',
'DealPrice',
[
'label' => 'Product Name',
'attribute' => 'vendorProducts.product.ProductName',
],
],
]);
使用普通的连接查询很简单,但我需要使用
$query
类来实现这一点。编辑:
简言之,我想要下面的ActiveRecord查询的结果。
SELECT Deals.*, VendorProduct.VPid, Product.ProductName
FROM Deals
JOIN VendorProduct ON VendorProduct.VPid = Deals.VPid
JOIN Product ON Product.ProductId = VendorProduct.ProductId;
当我在视图中打印
ActiveDataProvider
时,得到了以下数组。(为了可读性,我删除了额外的列)。
backend\models\VendorProducts Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[VPid] => 3
[ProductId] => 4175
[VPprice] => 0
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
[product] => backend\models\Product Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[ProductId] => 4175
[ProductName] => Human Resource Consultancy
)
)
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
最佳答案
网格视图
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'vendorProduct.VPprice',
[
'label' => 'Product Name',
'attribute' => 'vendorProduct.product.ProductName',
],
],
]); ?>
关于php - Yii2-事件记录中的传递依赖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40216058/