Skip to content
Permalink
Browse files

Added 'obj' back to the fetch and fetchAll methods.

  • Loading branch information...
T0mats0 committed Apr 5, 2018
1 parent 7447f1d commit 142936493ecc4eedd5eb1bd5d348bb21caeb6d1c
@@ -96,6 +96,9 @@ public function fetch($type = parent::FETCH_TYPE_NUM)
if ($type === static::FETCH_TYPE_ASSOC) {
return $this->_statement->fetch(PDO::FETCH_ASSOC);
}
if ($type === static::FETCH_TYPE_OBJ) {
return $this->_statement->fetch(PDO::FETCH_OBJ);
}
return $this->_statement->fetch($type);
}
@@ -122,6 +125,9 @@ public function fetchAll($type = parent::FETCH_TYPE_NUM)
if ($type === static::FETCH_TYPE_ASSOC) {
return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
}
if ($type === static::FETCH_TYPE_OBJ) {
return $this->_statement->fetch(PDO::FETCH_OBJ);
}
return $this->_statement->fetchAll($type);
}
@@ -42,11 +42,17 @@ class StatementDecorator implements StatementInterface, Countable, IteratorAggre
*/
const FETCH_TYPE_NUM = 'num';
/**
* Used to designate that associated array be returned in a result when calling fetch methods
* Used to designate that an associated array be returned in a result when calling fetch methods
*
* @var string
*/
const FETCH_TYPE_ASSOC = 'assoc';
/**
* Used to designate that a stdClass object be returned in a result when calling fetch methods
*
* @var string
*/
const FETCH_TYPE_OBJ = 'obj';
/**
* Statement instance implementation, such as PDOStatement
@@ -17,6 +17,7 @@
use Cake\Database\ExpressionInterface;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Query;
use Cake\Database\Statement\StatementDecorator;
use Cake\Database\StatementInterface;
use Cake\Database\TypeMap;
use Cake\Datasource\ConnectionManager;
@@ -4699,6 +4700,28 @@ public function testFetchAssoc()
$this->assertSame(['id' => 1, 'user_id' => 1, 'is_active' => false], $results);
}
/**
* Test that calling fetch with with FETCH_TYPE_OBJ return stdClass object.
* @return void
* @throws \Exception
*/
public function testFetchObjects()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$results = $query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles')
->limit(1)
->execute()
->fetch(StatementDecorator::FETCH_TYPE_OBJ);
$this->assertInstanceOf(\stdClass::class, $results);
}
/**
* Test that fetchColumn() will return the correct value at $position.
* @throws \Exception

0 comments on commit 1429364

Please sign in to comment.
You can’t perform that action at this time.