Skip to content

Commit

Permalink
Added 'obj' back to the fetch and fetchAll methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Ritter committed Apr 5, 2018
1 parent 7447f1d commit 1429364
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Database/Statement/PDOStatement.php
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Database/Statement/StatementDecorator.php
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1429364

Please sign in to comment.