尝试使用 php 更改数据库状态,假设状态为 1,则已处理。所以我需要获取 stae 0 的所有数据并更改为 1。
array(3) {["sender"]=>string(6 ) "wandia"["receiver"]=>string(4) "john"["user_id"]=>int(1)}
array(3) {["sender"]=>string(11) "test_sender "["receiver"]=>字符串(13) "test_receiver"
["user_id"]=>int(123http://127.0.0.1:8080/inbox/give): 无法打开流:HTTP 请求失败!"}
public function actionProcess()
{
Yii::$app->response->format = Response::FORMAT_JSON;
try {
// Fetch outbox data with state = 0
$outboxData = Yii::$app->db->createCommand('
SELECT *
FROM inbox
WHERE state = "0"
')
->queryAll();
foreach ($outboxData as $data) {
// Create JSON payload
$payload = [
'sender' => $data['sender'],
'receiver' => $data['receiver'],
'user_id' => $data['user_id'],
// Add other fields as needed
];
echo '<pre>';
var_dump($payload);
echo '</pre>';
}
$http = new Client();
//Send POST request to outbox/give
$response = $http->createRequest()
->setMethod('POST')
->setUrl(['http://127.0.0.1:8080/inbox/give'])
->setData($payload)
->send();
// Check if the request was successful
if ($response->isOk) {
// Update outbox state to 1 and save
Yii::$app->db->createCommand('
UPDATE inbox
SET state = "1"
WHERE id = :id
')
->bindValue(':id', $data['id'])
->execute();
}
return ['status' => 'ok', 'description' => 'success'];
// Return success response
}catch (Exception $e) {
// Handle exceptions
return ['status' => 'error', 'description' => $e->getMessage()];
}
}
我认为你有一个名为
Inbox
的模型,所以你可以这样做:
$inbox = InboxModel::updateAll([
'state' => true,
], ['id' => 1]); # condition
这样,yii 准备 sql 并使用 AR 方法以正确的方式执行它