【问题标题】:Yii2 - trasitive dependency in active recordsYii2 - 活动记录中的传递依赖
【发布时间】:2016-10-24 10:24:31
【问题描述】:

我有 3 张桌子。

Product [ProductId, ProductName], VendorProduct[VPid, ProductId, VPprice], Deals[DealId, VPid, DealPrice]

我想在 GridView 中使用活动记录显示交易。我定义了关系,但无法从表 Product 中访问 ProductName。

Deals.*, VendorProduct.VPid, Product.ProductName

这就是我想要的结果。 我的 DealsSearch 模型是这样的。

$query = Deals::find()->joinWith(['vendorProduct', 'vendorProduct.product']);

然后我像这样将$query 传递给ActiveDataProvider

$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',
        ],
    ],
]);

普通的连接查询很简单,但我需要使用ActiveRecord 类。

编辑:

简而言之,我想要使用 ActiveRecord 进行以下查询的结果。

SELECT Deals.*, VendorProduct.VPid, Product.ProductName
FROM Deals
JOIN VendorProduct ON VendorProduct.VPid = Deals.VPid
JOIN Product ON Product.ProductId = VendorProduct.ProductId;

当我在我的视图中打印$model->vendorProduct 时,我得到了以下数组。 (为了便于阅读,我删除了额外的列)。

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
        (
        )

)

【问题讨论】:

  • 不清楚你在问什么。
  • @InsaneSkull :我编辑了问题以供理解。
  • 如果你想得到活动记录,只需在查询后使用all()
  • @InsaneSkull :我在视图中打印了该结果,它显示了数组中的数据,但我可以访问它。它正在显示数据,但不与 GridView 同步。
  • 你想在gridview中显示数据?

标签: php mysql activerecord yii2


【解决方案1】:

网格视图

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'vendorProduct.VPprice',
        [
          'label' => 'Product Name',
          'attribute' => 'vendorProduct.product.ProductName',
        ],
    ],
]); ?>

【讨论】:

  • 我在发布问题之前尝试了相同的逻辑,但它不起作用。
  • @InsaneSkull:我在我的问题中添加了输出数组(不可能在 cmets 中添加它)。
  • @kishor10d。那么您的代码也没有任何错误,请更新您使用 gridview 尝试的方式。
  • @InsaneSkull :我用 GridView 的代码更新了问题。
  • @kishor10d。将attribute 替换为value
【解决方案2】:

您是否尝试过如下使用自定义值..

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'DealId',
        'VPid',
        'DealPrice',
        [
          'label' => 'Product Name',
          'attribute' => 'vendorProducts.product.ProductName', // here use attribute ProductName, 
          'value'=> 'vendorProducts.product.ProductName',
        ],
    ],
]);

【讨论】:

  • 不,它会产生错误trying to access property of non-object
  • 参考更新的答案,我不确定attributeProductName还是别的什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
相关资源
最近更新 更多