Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add DBAL\TypeAwareObject type inference. #432

wants to merge 1 commit into


None yet
4 participants

DBAL allows you to define custom field types for your entities, and those are seamlessly converted from PHP to SQL value. However, you can't those custom types as parameters without type hinting it :

   ->from('Entity', 'e')
   ->where('e.customField = :customFieldValue')
   //this third argument is for now compulsory

In my case, $customFieldValue is an object that won't work well if converted with the default string type. I added a new DBAL interface (see doctrine/dbal#193 ) and tweaked the parameter type inference so that custom values can advertise their DBAL type.

There is currently no way to dynamically override the parameter type inference logic, this is one design that allows it in some cases.

@stof stof commented on the diff Aug 29, 2012

@@ -60,6 +61,10 @@ public static function inferType($value)
: Connection::PARAM_STR_ARRAY;
+ if ($value instanceof TypeAwareObject) {
+ return $value->getDBALType();

stof Aug 29, 2012


The indentation need to be fixed


beberlei commented Aug 29, 2012

Its a nice idea, but i don't think its the right solution


beberlei commented Aug 29, 2012

I'd rather make the type inferrer configurable somehow.

@beberlei What to you think about defining getParameterTypeInferer and setParameterTypeInferer getter/setter in QueryBuilder (and thus making ParameterTypeInferer::inferType a method instead of a static class function) ? Thus a query builder could use a curstom inferer.


stof commented Aug 29, 2012

I don't think it should be registered in the query builder. It would require registering it each time

This pull request passes (merged 0ce3728 into bc2476f).

@stof @beberlei Maybe we can define getParameterTypeInferer and setParameterTypeInferer at the EntityManager level, then in QueryBuilder :

public function setParameter($key, $value, $type = null)
    $type = $this->_em->getParameterTypeInferer()->inferType($value);

I also noted on another comment from @stof that 2.2 branch now only gets bug fixes. Does it mean this feature, if implemented in a way or in another, will never be included to the latest 2.2 branch ? Any chance that it get's included to the 2.3 branch currently in release candidate ?


stof commented Aug 29, 2012

@Romain-Geissler RC means it does not receive features either. It is too late in the release cycle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment