return $entity;
}
+ /**
+ * Checks if an array is associative
+ * @param array $array
+ * @return bool true if associative
+ */
+ private function isAssocArray(array $array) {
+ return array_values($array) !== $array;
+ }
+
+ /**
+ * Returns the correct PDO constant based on the value type
+ * @param $value
+ * @return PDO constant
+ */
+ private function getPDOType($value) {
+ switch (gettype($value)) {
+ case 'integer':
+ return \PDO::PARAM_INT;
+ case 'boolean':
+ return \PDO::PARAM_BOOL;
+ default:
+ return \PDO::PARAM_STR;
+ }
+ }
+
/**
* Runs an sql query
$query = $this->db->prepare($sql, $limit, $offset);
}
- $index = 1; // bindParam is 1 indexed
- foreach($params as $param) {
-
- switch (gettype($param)) {
- case 'integer':
- $pdoConstant = \PDO::PARAM_INT;
- break;
-
- case 'boolean':
- $pdoConstant = \PDO::PARAM_BOOL;
- break;
-
- default:
- $pdoConstant = \PDO::PARAM_STR;
- break;
+ if ($this->isAssocArray($params)) {
+ foreach ($params as $key => $param) {
+ $pdoConstant = $this->getPDOType($param);
+ $query->bindValue($key, $param, $pdoConstant);
+ }
+ } else {
+ $index = 1; // bindParam is 1 indexed
+ foreach ($params as $param) {
+ $pdoConstant = $this->getPDOType($param);
+ $query->bindValue($index, $param, $pdoConstant);
+ $index++;
}
-
- $query->bindValue($index, $param, $pdoConstant);
-
- $index++;
}
$result = $query->execute();
public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
public function findAllEntities($table){ return $this->findEntities($table); }
public function mapRow($row){ return $this->mapRowToEntity($row); }
+ public function execSql($sql, $params){ return $this->execute($sql, $params); }
}
}
+ public function testAssocParameters() {
+ $sql = 'test';
+ $params = [':test' => 1, ':a' => 2];
+
+ $this->setMapperResult($sql, $params);
+ $this->mapper->execSql($sql, $params);
+ }
+
+
public function testUpdate(){
$sql = 'UPDATE `*PREFIX*table` ' .
'SET ' .
$this->fetchAt = 0;
}
+ /**
+ * Checks if an array is associative
+ * @param array $array
+ * @return bool true if associative
+ */
+ private function isAssocArray(array $array) {
+ return array_values($array) !== $array;
+ }
+ /**
+ * Returns the correct PDO constant based on the value type
+ * @param $value
+ * @return PDO constant
+ */
+ private function getPDOType($value) {
+ switch (gettype($value)) {
+ case 'integer':
+ return \PDO::PARAM_INT;
+ case 'boolean':
+ return \PDO::PARAM_BOOL;
+ default:
+ return \PDO::PARAM_STR;
+ }
+ }
/**
* Create mocks and set expected results for database queries
}
));
- $index = 1;
- foreach($arguments as $argument) {
- switch (gettype($argument)) {
- case 'integer':
- $pdoConstant = \PDO::PARAM_INT;
- break;
-
- case 'NULL':
- $pdoConstant = \PDO::PARAM_NULL;
- break;
-
- case 'boolean':
- $pdoConstant = \PDO::PARAM_BOOL;
- break;
-
- default:
- $pdoConstant = \PDO::PARAM_STR;
- break;
+ if ($this->isAssocArray($arguments)) {
+ foreach($arguments as $key => $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($key),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $this->queryAt++;
+ }
+ } else {
+ $index = 1;
+ foreach($arguments as $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($index),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $index++;
+ $this->queryAt++;
}
- $this->query->expects($this->at($this->queryAt))
- ->method('bindValue')
- ->with($this->equalTo($index),
- $this->equalTo($argument),
- $this->equalTo($pdoConstant));
- $index++;
- $this->queryAt++;
}
$this->query->expects($this->at($this->queryAt))