2016-11-27 24 views
0

我碰到一個問題,讓它正常工作正在融化我的大腦。我想知道有沒有人能指出我的方向。PHP - Message Queue - 使用提取作爲參考的PDO bindParam

基本上,我使用多線程消息隊列處理程序將消息值插入到數據庫中。一切工作正常,甚至下面的代碼,但只在最初的插入。變量綁定後,它們保持相同的值,並且不會引用$ json對象中的更改。我嘗試了幾種不同的方式,但我似乎無法獲得工作的參考。任何幫助,將不勝感激!

private function handle_insert($message) { 
    // declare data 
    $json = json_decode($message->body); 
    // prepare the statement 
    if (!isset($this->statement)) $this->prepare_statement(); 
    // if there are no bindings 
    if (!$this->binding) { 
     // extract params 
     extract(get_object_vars($json), EXTR_REFS); 
     // loop over data 
     foreach ($json as $key => $value) { 
      // bind data 
      $this->statement->bindParam(":{$key}", $$key, $this->pdo_param($$key)); // using function for defined types 
     } 
     // params are bound 
     $this->binding = true; 
    } 
    // execute the statement 
    $this->statement->execute(); 
} 

我可以bindParam每個插入,甚至使用bindValue。然而,bindParam不是隻綁定一次,然後改變這個值,減少循環的需要嗎?

回答

-1

它看起來像類對象保持的變量$這個 - >結合,你已經在你的第一個電話已經被設置爲

$this->binding = true; 

一旦綁定值在第一次調用設置,那麼以下條件阻止了進一步的狀態在隨後的調用中將新變量綁定到PDO對象。

if (!$this->binding) 
+0

這是這個變量的目的:保持狀態並阻止綁定 –

0

我認爲這是一個有趣的問題,儘管與PDO無關。

您必須考慮提取的變量所引用的值。然後考慮代碼中過度工程的級別。

然後將extract()移到條件之外。