From 91b0e9817151d7e9ac23270702ae12c692a382cd Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 6 Jun 2017 09:48:40 +0200 Subject: [PATCH] DbRepository: Allow to pass parameter $types to methods insert() and update() DbConnection does already support this but it got somehow forgotten in this class. --- library/Icinga/Repository/DbRepository.php | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Repository/DbRepository.php b/library/Icinga/Repository/DbRepository.php index dfc600a4b..c30d89d55 100644 --- a/library/Icinga/Repository/DbRepository.php +++ b/library/Icinga/Repository/DbRepository.php @@ -348,29 +348,43 @@ abstract class DbRepository extends Repository implements Extensible, Updatable, /** * Insert a table row with the given data * + * Note that the base implementation does not perform any quoting on the $table argument. + * Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value + * as third parameter $types to define a different type than string for a particular column. + * * @param string $table * @param array $bind + * @param array $types * * @return int The number of affected rows */ - public function insert($table, array $bind) + public function insert($table, array $bind, array $types = array()) { - return $this->ds->insert( - $this->clearTableAlias($this->requireTable($table)), - $this->requireStatementColumns($table, $bind) - ); + $realTable = $this->clearTableAlias($this->requireTable($table)); + + foreach ($types as $alias => $type) { + unset($types[$alias]); + $types[$this->requireStatementColumn($table, $alias)] = $type; + } + + return $this->ds->insert($realTable, $this->requireStatementColumns($table, $bind), $types); } /** * Update table rows with the given data, optionally limited by using a filter * + * Note that the base implementation does not perform any quoting on the $table argument. + * Pass an array with a column name (the same as in $bind) and a PDO::PARAM_* constant as value + * as fourth parameter $types to define a different type than string for a particular column. + * * @param string $table * @param array $bind * @param Filter $filter + * @param array $types * * @return int The number of affected rows */ - public function update($table, array $bind, Filter $filter = null) + public function update($table, array $bind, Filter $filter = null, array $types = array()) { $realTable = $this->clearTableAlias($this->requireTable($table)); @@ -378,7 +392,12 @@ abstract class DbRepository extends Repository implements Extensible, Updatable, $filter = $this->requireFilter($table, $filter); } - return $this->ds->update($realTable, $this->requireStatementColumns($table, $bind), $filter); + foreach ($types as $alias => $type) { + unset($types[$alias]); + $types[$this->requireStatementColumn($table, $alias)] = $type; + } + + return $this->ds->update($realTable, $this->requireStatementColumns($table, $bind), $filter, $types); } /**