我有以下查询:
$transactionIds = \Yii::app()->db->createCommand()
->select("transactionId")
->from("batch_lab_transactions")
->where("batchId = :batchId", ["batchId" => $batch->id])
->queryAll();
根据\Yii::log(json_encode($transactionIds));
显示:
[{ “的transactionId”: “658000024”},{ “的transactionId”: “658000025”}]
我正在尝试访问transactionId
密钥,但我一直收到错误消息,说我正在尝试访问非对象。这是我使用的循环:
foreach ($transactionIds as $transaction) {
$transaction->transactionId // Error
}
答案 0 :(得分:1)
您将获得包含此代码的数组数组。
所以你可以使用$transaction['transactionId']
在这种情况下,使用queryColumn()
代替queryAll()
的更好方法是:
$transactionIds = \Yii::app()->db->createCommand()
->select("transactionId")
->from("batch_lab_transactions")
->where("batchId = :batchId", ["batchId" => $batch->id])
->queryColumn(); // <-- change here
foreach ($transactionIds as $transactionId) {
echo $transactionId;
}