如何访问从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;
答案 0 :(得分:0)
通过查询,您可以存储在数组$data
$data['categoryMaster']= $categoryMaster;
您可以遍历数组以检索每个模型并访问选定的列
<?php foreach ($data as $key => $value){
echo "Key : ".$key. "<br />";
echo $value->categoryMaster_id ."<br />";
echo $value->categoryMaster_name ."<br />"
.....
echo $value->productQuantity ."<br />"
}
答案 1 :(得分:0)
如果您在select中使用自定义列,则可能应该使用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'],
];
});