yii2 - 如何从 Yii2 中的 select() 访问值?
问题描述
如何访问从select()
. 我想要所有链接表中的所有 id。我categoryMaster_id
作为参数传递,因此我需要剩余的 id。
这是我到目前为止所做的:
$categoryMaster= (new \yii\db\Query())
->select('categoryMaster.categoryMaster_id,categoryMaster_name,categoryMaster_image,
productType.productType_id,productType.productType_type,
product.product_id,product.product_name,product.product_description,
productQuantity.productQuantity_id,productQuantity.productQuantity_name,productQuantity.productQuantity')
->from('categoryMaster')
->join('LEFT JOIN','productType', 'productType.categoryMaster_id = categoryMaster.categoryMaster_id')
->join('LEFT JOIN','product','product.productType_id = productType.productType_id')
->join('LEFT JOIN','productQuantity','productQuantity.product_id = product.product_id')
->where('categoryMaster.categoryMaster_id=:categoryMaster_id', ['categoryMaster_id' => $_GET['categoryMaster_id']])
->all();
$data['categoryMaster']= $categoryMaster;
解决方案
如果您在选择中使用自定义列,您可能应该使用asArray()
- 查询将结果作为数组而不是对象返回。
要从结果中提取指定的字段,您可以使用ArrayHelper::getColumn()
:
$result = (new \yii\db\Query())
->select('categoryMaster.categoryMaster_id,categoryMaster_name,categoryMaster_image,
productType.productType_id,productType.productType_type,
product.product_id,product.product_name,product.product_description,
productQuantity.productQuantity_id,productQuantity.productQuantity_name,productQuantity.productQuantity')
->from('categoryMaster')
->join('LEFT JOIN','productType', 'productType.categoryMaster_id = categoryMaster.categoryMaster_id')
->join('LEFT JOIN','product','product.productType_id = productType.productType_id')
->join('LEFT JOIN','productQuantity','productQuantity.product_id = product.product_id')
->where('categoryMaster.categoryMaster_id=:categoryMaster_id', ['categoryMaster_id' => $_GET['categoryMaster_id']])
->asArray()
->all();
$categoryMasterIds = ArrayHelper::getColumn($result, 'categoryMaster_id');
$productTypeIds = ArrayHelper::getColumn($result, 'productType_id');
或者使用闭包来提取 touple:
$productTypes = ArrayHelper::getColumn($result, function ($data) {
return [
'productType_id' => $data['productType_id'],
'productType_type' => $data['productType_type'],
];
});
推荐阅读
- git - 为什么“git fetch origin bugfix”不起作用?
- apache-kafka - kafka connect hdfs sink 滞后监控
- openlayers - 如何将 GeoServer 上的 WMS(Web 地图服务)覆盖到 Openlayers 地图
- android - 蓝牙不会在后台模式下扫描附近的设备 android 11+
- fullcalendar - 未在完整日历上显示的事件
- c++ - 我的代码编译得很好,但在 geeksforgeeks 上提交时显示分段错误.....我无法识别问题
- go - 一个 CustomType protobuf
- r - 在 R 中添加特定列值
- c# - 针对 Intranet 应用程序的 Active Directory 安全建议/洞察
- torch - [libtorch][torch.jit.script][输入动态形状]加载“. C++ 上的 pt” 文件:索引超出范围