Doctrine\DBAL\Connection::update PHP Method

update() public method

Table expression and columns are not escaped and are not safe for user-input.
public update ( string $tableExpression, array $data, array $identifier, array $types = [] ) : integer
$tableExpression string The expression of the table to update quoted or unquoted.
$data array An associative array containing column-value pairs.
$identifier array The update criteria. An associative array containing column-value pairs.
$types array Types of the merged $data and $identifier arrays in that order.
return integer The number of affected rows.
    public function update($tableExpression, array $data, array $identifier, array $types = array())
    {
        $set = array();
        foreach ($data as $columnName => $value) {
            $set[] = $columnName . ' = ?';
        }
        if (is_string(key($types))) {
            $types = $this->extractTypeValues(array_merge($data, $identifier), $types);
        }
        $params = array_merge(array_values($data), array_values($identifier));
        $sql = 'UPDATE ' . $tableExpression . ' SET ' . implode(', ', $set) . ' WHERE ' . implode(' = ? AND ', array_keys($identifier)) . ' = ?';
        return $this->executeUpdate($sql, $params, $types);
    }

Usage Example

 /**
  * Saves the pool to the database.
  *
  * @param \MusicBox\Entity\Like $pool
  */
 public function save($pool)
 {
     $poolData = array('address_id' => $pool->getAddress()->getId(), 'access_info' => $pool->getAccessInfo());
     if ($pool->getId()) {
         $this->db->update('pools', $poolData, array('pool_id' => $pool->getId()));
         $newFile = $this->handleFileUpload($item);
         if ($newFile) {
             $poolData['image'] = $pool->getImage();
         }
     } else {
         // The pool is new, note the creation timestamp.
         $poolData['created_at'] = time();
         $this->db->insert('pools', $poolData);
         // Get the id of the newly created pool and set it on the entity.
         $id = $this->db->lastInsertId();
         $pool->setId($id);
         // If a new image was uploaded, update the pool with the new
         // filename.
         $newFile = $this->handleFileUpload($pool);
         if ($newFile) {
             $newData = array('image' => $pool->getImage());
             $this->db->update('pools', $newData, array('pool_id' => $id));
         }
     }
 }
All Usage Examples Of Doctrine\DBAL\Connection::update