If the execute method on the mapper receives an assoc array, it binds by value instead of index
This commit is contained in:
parent
857b22c61b
commit
df24a014b8
|
@ -184,6 +184,31 @@ abstract class Mapper {
|
||||||
return $entity;
|
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
|
* Runs an sql query
|
||||||
|
@ -200,26 +225,18 @@ abstract class Mapper {
|
||||||
$query = $this->db->prepare($sql, $limit, $offset);
|
$query = $this->db->prepare($sql, $limit, $offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
$index = 1; // bindParam is 1 indexed
|
if ($this->isAssocArray($params)) {
|
||||||
foreach($params as $param) {
|
foreach ($params as $key => $param) {
|
||||||
|
$pdoConstant = $this->getPDOType($param);
|
||||||
switch (gettype($param)) {
|
$query->bindValue($key, $param, $pdoConstant);
|
||||||
case 'integer':
|
}
|
||||||
$pdoConstant = \PDO::PARAM_INT;
|
} else {
|
||||||
break;
|
$index = 1; // bindParam is 1 indexed
|
||||||
|
foreach ($params as $param) {
|
||||||
case 'boolean':
|
$pdoConstant = $this->getPDOType($param);
|
||||||
$pdoConstant = \PDO::PARAM_BOOL;
|
$query->bindValue($index, $param, $pdoConstant);
|
||||||
break;
|
$index++;
|
||||||
|
|
||||||
default:
|
|
||||||
$pdoConstant = \PDO::PARAM_STR;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->bindValue($index, $param, $pdoConstant);
|
|
||||||
|
|
||||||
$index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
|
|
|
@ -47,6 +47,7 @@ class ExampleMapper extends Mapper {
|
||||||
public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
|
public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
|
||||||
public function findAllEntities($table){ return $this->findEntities($table); }
|
public function findAllEntities($table){ return $this->findEntities($table); }
|
||||||
public function mapRow($row){ return $this->mapRowToEntity($row); }
|
public function mapRow($row){ return $this->mapRowToEntity($row); }
|
||||||
|
public function execSql($sql, $params){ return $this->execute($sql, $params); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -187,6 +188,15 @@ class MapperTest extends MapperTestUtility {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testAssocParameters() {
|
||||||
|
$sql = 'test';
|
||||||
|
$params = [':test' => 1, ':a' => 2];
|
||||||
|
|
||||||
|
$this->setMapperResult($sql, $params);
|
||||||
|
$this->mapper->execSql($sql, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testUpdate(){
|
public function testUpdate(){
|
||||||
$sql = 'UPDATE `*PREFIX*table` ' .
|
$sql = 'UPDATE `*PREFIX*table` ' .
|
||||||
'SET ' .
|
'SET ' .
|
||||||
|
|
|
@ -56,7 +56,30 @@ abstract class MapperTestUtility extends \Test\TestCase {
|
||||||
$this->fetchAt = 0;
|
$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
|
* Create mocks and set expected results for database queries
|
||||||
|
@ -117,32 +140,28 @@ abstract class MapperTestUtility extends \Test\TestCase {
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
$index = 1;
|
if ($this->isAssocArray($arguments)) {
|
||||||
foreach($arguments as $argument) {
|
foreach($arguments as $key => $argument) {
|
||||||
switch (gettype($argument)) {
|
$pdoConstant = $this->getPDOType($argument);
|
||||||
case 'integer':
|
$this->query->expects($this->at($this->queryAt))
|
||||||
$pdoConstant = \PDO::PARAM_INT;
|
->method('bindValue')
|
||||||
break;
|
->with($this->equalTo($key),
|
||||||
|
$this->equalTo($argument),
|
||||||
case 'NULL':
|
$this->equalTo($pdoConstant));
|
||||||
$pdoConstant = \PDO::PARAM_NULL;
|
$this->queryAt++;
|
||||||
break;
|
}
|
||||||
|
} else {
|
||||||
case 'boolean':
|
$index = 1;
|
||||||
$pdoConstant = \PDO::PARAM_BOOL;
|
foreach($arguments as $argument) {
|
||||||
break;
|
$pdoConstant = $this->getPDOType($argument);
|
||||||
|
$this->query->expects($this->at($this->queryAt))
|
||||||
default:
|
->method('bindValue')
|
||||||
$pdoConstant = \PDO::PARAM_STR;
|
->with($this->equalTo($index),
|
||||||
break;
|
$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))
|
$this->query->expects($this->at($this->queryAt))
|
||||||
|
|
Loading…
Reference in New Issue