From 2c3cf655e04859d0284a79997213616cb5e5282a Mon Sep 17 00:00:00 2001 From: Fabian de Laender Date: Sun, 29 May 2016 12:56:40 +0200 Subject: [PATCH] Release version 1.9.9 --- CHANGELOG.md | 10 ++++++++++ composer.json | 2 +- src/PDO/Database.php | 6 +++--- src/PDO/Statement/InsertStatement.php | 11 ++++++++--- src/PDO/Statement/StatementContainer.php | 12 +++++++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a896004..9ab7965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ ### Changelog +##### v1.9.9 ++ Added ability to insert associative arrays (#35) ++ Updated `Database` class with: + - Renamed `$columns` argument in `insert()` method ++ Updated `StatementContainer` class with: + - Added `isAssociative()` method + - Fixed `getPlaceholders()` method + +> Proposed by [Raistlfiren](https://github.com/Raistlfiren). Thanks! + ##### v1.9.8 + Updated `SelectStatement` class with: - Fixed `getColumns()` method diff --git a/composer.json b/composer.json index dca7888..52677a5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "slim/pdo", "description": "PDO database library for Slim Framework", - "version": "1.9.8", + "version": "1.9.9", "type": "library", "keywords": ["pdo", "database", "slim", "framework"], "homepage": "https://github.com/FaaPz/Slim-PDO", diff --git a/src/PDO/Database.php b/src/PDO/Database.php index 8cdef7e..28ca3cc 100644 --- a/src/PDO/Database.php +++ b/src/PDO/Database.php @@ -57,13 +57,13 @@ public function select(array $columns = array('*')) } /** - * @param array $columns + * @param array $columnsOrPairs * * @return InsertStatement */ - public function insert(array $columns = array()) + public function insert(array $columnsOrPairs = array()) { - return new InsertStatement($this, $columns); + return new InsertStatement($this, $columnsOrPairs); } /** diff --git a/src/PDO/Statement/InsertStatement.php b/src/PDO/Statement/InsertStatement.php index 251bbb1..56ee140 100644 --- a/src/PDO/Statement/InsertStatement.php +++ b/src/PDO/Statement/InsertStatement.php @@ -19,13 +19,18 @@ class InsertStatement extends StatementContainer * Constructor. * * @param Database $dbh - * @param array $columns + * @param array $columnsOrPairs */ - public function __construct(Database $dbh, array $columns) + public function __construct(Database $dbh, array $columnsOrPairs) { parent::__construct($dbh); - $this->columns($columns); + if ($this->isAssociative($columnsOrPairs)) { + $this->columns(array_keys($columnsOrPairs)); + $this->values(array_values($columnsOrPairs)); + } else { + $this->columns($columnsOrPairs); + } } /** diff --git a/src/PDO/Statement/StatementContainer.php b/src/PDO/Statement/StatementContainer.php index 3fd19db..5986e91 100644 --- a/src/PDO/Statement/StatementContainer.php +++ b/src/PDO/Statement/StatementContainer.php @@ -457,7 +457,7 @@ protected function getPlaceholders() { $placeholders = $this->placeholders; - unset($this->placeholders); + reset($this->placeholders); return '( '.implode(' , ', $placeholders).' )'; } @@ -472,6 +472,16 @@ protected function setPlaceholders(array $values) } } + /** + * @param array $array + * + * @return bool + */ + protected function isAssociative(array $array) + { + return array_keys($array) !== range(0, count($array) - 1); + } + /** * @return \PDOStatement */