【问题标题】:Symfony2 + DBAL. How to use bindValue for multiple insert?Symfony2 + DBAL。如何使用 bindValue 进行多次插入?
【发布时间】:2016-07-03 22:04:39
【问题描述】:

我正在使用 DBAL,我想执行多个插入查询。但我有问题:bindValue() 方法不能循环工作。这是我的代码:

    $insertQuery = "INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES %s 
           ON DUPLICATE KEY UPDATE company=VALUES(company), user=VALUES(user)";

    for ($i = 0; $i < count($data); $i++) {
        $inserted[] = "(':number', ':company', ':user')";
    }

    $insertQuery = sprintf($insertQuery, implode(",", $inserted));
    $result = $db->getConnection()->prepare($insertQuery);

    for ($i = 0; $i < count($data); $i++) {
        $result->bindValue($data[$i]["number"]);
        $result->bindValue($data[$i]["company"]);
        $result->bindValue($data[$i]["user"]);
    }

    $result->execute();

结果我收到了包含字段的单行表::number:company:user

我做错了什么?

非常感谢您的帮助!

【问题讨论】:

    标签: symfony dbal


    【解决方案1】:

    还有另一种选择:

    $queryStart = "INSERT INTO {$tableName} (" . implode(', ', array_keys($buffer[0])) . ") VALUES ";
    $queryRows = $params = $types = [];
    
    foreach ($rowBuffer as $row) {
        $rowQuery =  '(' . implode(', ', array_fill(0, count($row), '?')) . ')';
        $rowParams = array_values($row);
    
        list($rowQuery, $rowParams, $types) = SQLParserUtils::expandListParameters($rowQuery, $rowParams, $types);
    
        $queryRows[] = $rowQuery;
        $params = array_merge($params, $rowParams);
    }
    
    $query = $queryStart . implode(', ', $queryRows);
    
    $connection->executeQuery($query, $params, $types);
    

    【讨论】:

      【解决方案2】:

      您遇到的问题是您的绑定无法确定它应该与哪个占位符进行绑定。为了更好地对其进行可视化,请考虑您正在生成的最终 DBAL 查询:

      INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES
      (':number', ':company', ':user'),
      (':number', ':company', ':user'),
      (':number', ':company', ':user');
      

      当您进行绑定时,您将同时替换所有参数,最终插入一行。

      一种可能的解决方案是为每一行指定不同的参数名称,然后相应地替换每个参数名称。 它看起来像这样:

      public function randomParameterName()
      {
          return uniqid('param_');
      }
      
      ...
      
      $parameters = [];
      for ($i = 0; $i < count($data); $i++) {
          $parameterNames = [
              'number' => $this->randomParameterName(),
              'company' => $this->randomParameterName(), 
              'user' => $this->randomParameterName(),
          ];
          $parameters[$i] = $parameterNames;
          $inserted[] = sprintf("(':%s', ':%s', ':%s')",
              $parameterNames['number'], 
              $parameterNames['company'], 
              $parameterNames['user']
          );
      }
      
      $insertQuery = sprintf($insertQuery, implode(",", $inserted));
      $result = $db->getConnection()->prepare($insertQuery);
      
      foreach ($parameters as $i => $parameter) {
          $result->bindValue($parameter['number'], $data[$i]["number"]);
          $result->bindValue($parameter['company'], $data[$i]["company"]);
          $result->bindValue($parameter['user'], $data[$i]["user"]);
      }
      

      您可以扩展您的$data 变量并将新参数名称合并到其中。这将不再需要另一个数组 $parameters 来保存对新创建的参数名称的引用。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-14
        • 2012-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 2018-02-27
        • 2012-12-10
        相关资源
        最近更新 更多