Skip to content
Permalink
Browse files

Added a way to disable type casting in the queries

  • Loading branch information...
lorenzo committed Feb 24, 2018
1 parent 5d0cb02 commit b577ed3c26983627598a2159226dda2f0ad6549b
Showing with 36 additions and 11 deletions.
  1. +36 −11 src/Database/Query.php
@@ -132,11 +132,11 @@ class Query implements ExpressionInterface, IteratorAggregate
protected $_selectTypeMap;
/**
* Tracking flag to ensure only one type caster is appended.
* Tracking flag to disable casting
*
* @var bool
*/
protected $_typeCastAttached = false;
protected $typeCastEnabled = true;
/**
* Constructor.
@@ -217,14 +217,6 @@ public function connection($connection = null)
public function execute()
{
$statement = $this->_connection->run($this);
$typeMap = $this->getSelectTypeMap();
if ($typeMap->toArray() && $this->_typeCastAttached === false) {
$driver = $this->_connection->getDriver();
$this->decorateResults(new FieldTypeConverter($typeMap, $driver));
$this->_typeCastAttached = true;
}
$this->_iterator = $this->_decorateStatement($statement);
$this->_dirty = false;
@@ -2026,6 +2018,7 @@ public function setSelectTypeMap(TypeMap $typeMap)
return $this;
}
/**
* Gets the TypeMap class where the types for each of the fields in the
* select clause are stored.
@@ -2041,6 +2034,30 @@ public function getSelectTypeMap()
return $this->_selectTypeMap;
}
/**
* Disables the automatic casting of fields to their corresponding type
*
* @return $this
*/
public function disableResultsCasting()
{
$this->typeCastEnabled = false;
return $this;
}
/**
* Enables the automatic casting of fields to their corresponding type
*
* @return $this
*/
public function enableResultsCasting()
{
$this->typeCastEnabled = true;
return $this;
}
/**
* Sets the TypeMap class where the types for each of the fields in the
* select clause are stored.
@@ -2073,8 +2090,16 @@ public function selectTypeMap(TypeMap $typeMap = null)
*/
protected function _decorateStatement($statement)
{
$typeMap = $this->getSelectTypeMap();
$driver = $this->getConnection()->getDriver();
if ($this->typeCastEnabled && $typeMap->toArray()) {
$driver = $this->_connection->getDriver();
$statement = new CallbackStatement($statement, $driver, new FieldTypeConverter($typeMap, $driver));
}
foreach ($this->_resultDecorators as $f) {
$statement = new CallbackStatement($statement, $this->getConnection()->getDriver(), $f);
$statement = new CallbackStatement($statement, $driver, $f);
}
return $statement;

0 comments on commit b577ed3

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