From c7f5d9d77de7c3ffde23e841d815bb2440b994bf Mon Sep 17 00:00:00 2001 From: Josiah Truasheim Date: Fri, 31 Aug 2012 18:12:44 +0700 Subject: [PATCH] Fixed DDC-2003 using closures to reference the functionality of the calling entity persister from the SQL value visitor. --- .../ORM/Persisters/BasicEntityPersister.php | 10 +++- .../ORM/Persisters/SqlValueVisitor.php | 51 +++++++++++-------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index ed3485386..b96aec46c 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -808,7 +808,15 @@ class BasicEntityPersister return array(array(), array()); } - $valueVisitor = new SqlValueVisitor($this->_class); + $persister = $this; + $valueVisitor = new SqlValueVisitor( + function ($field, $value) use($persister) { + return $persister->getType($field, $value); + }, + function ($value) use($persister) { + return $persister->getValue($value); + } + ); $valueVisitor->dispatch($expression); return $valueVisitor->getParamsAndTypes(); diff --git a/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php b/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php index 043be12b8..beb41d9a6 100644 --- a/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php +++ b/lib/Doctrine/ORM/Persisters/SqlValueVisitor.php @@ -47,16 +47,35 @@ class SqlValueVisitor extends ExpressionVisitor private $types = array(); /** - * @var \Doctrine\ORM\Mapping\ClassMetadata + * Type Callback + * + * Called by this visitor to determine the type of a value + * + * @var callable */ - private $class; + private $typeCallback; /** - * @param \Doctrine\ORM\Mapping\ClassMetadata + * Value Callback + * + * Called by this visitor to generate a sql value from the given value + * + * Callback is passed two parameters: + * - `Field` name of the field in a comparison + * - `Value` value in a comparison + * + * @var callable */ - public function __construct(ClassMetadata $class) + private $valueCallback; + + /** + * @param callable $typeCallback Type Resolution Callback + * @param callable $valueCallback Value Resolution Callback + */ + public function __construct(callable $typeCallback, callable $valueCallback) { - $this->class = $class; + $this->typeCallback = $typeCallback; + $this->valueCallback = $valueCallback; } /** @@ -68,11 +87,14 @@ class SqlValueVisitor extends ExpressionVisitor */ public function walkComparison(Comparison $comparison) { + $typeCallback = $this->typeCallback; + $valueCallback = $this->valueCallback; + $value = $comparison->getValue()->getValue(); $field = $comparison->getField(); - - $this->values[] = $value; - $this->types[] = $this->getType($field, $value); + + $this->values[] = $valueCallback($value); + $this->types[] = $typeCallback($field, $value); } /** @@ -110,17 +132,4 @@ class SqlValueVisitor extends ExpressionVisitor { return array($this->values, $this->types); } - - private function getType($field, $value) - { - $type = isset($this->class->fieldMappings[$field]) - ? Type::getType($this->class->fieldMappings[$field]['type'])->getBindingType() - : \PDO::PARAM_STR; - - if (is_array($value)) { - $type += Connection::ARRAY_PARAM_OFFSET; - } - - return $type; - } }