Doctrine2 apply function to query parameter - doctrine-orm

I use the doctrine query builder to build my query.
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array('u'))->from('Account', 'a');
-- problem here
$qb->where('lower_unaccent(u.email) LIKE :search');
$qb->setParameter('search', $search['search'] . '%');
This works fine, but I would also like to apply the lower_unaccent function to the search parameter.
Is there a way to do this with the query builder?
Because when I do LOWER_UNACCENT(u.email) LIKE LOWER_UNACCENT(:search) I get the following error:
Error: Expected Doctrine\ORM\Query\Lexer::T_STRING, got 'LOWER_UNACCENT'
Even if I change LOWER_UNACCENT TO LOWER, I get the same error message.

You have to use $qb->expr()->lower('u.email') and $qb->expr()->lower(':search'). To create the "LIKE" pass those as arguments to $qb->expr()->like().
There doesn't seem to be a unaccent method though, but maybe there is a way to customize this. Let me know how it went.
EDIT:
Looking into lower()'s code, it seems like you can use any function you want like this:
new Expr\Func('LOWER', array($x));

By default, I'm pretty sure you can't do that.
lower_unaccent is not standard SQL, and as far as I can see, not supported by Doctrine.
What you can do is extend DQL with user-defined functions, which will do whatever you want. It is not so hard to implement. Here is some documentation:
http://docs.doctrine-project.org/en/2.0.x/cookbook/dql-user-defined-functions.html
http://docs.doctrine-project.org/en/2.0.x/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

Related

haystack query for solr to find items where attribute is unset or specified value

I'm trying to query solr through haystack for all objects that either does not have an attribute (it's Null) or the attribute is a specified value.
I can query solr directly with the snippet (brand:foo OR (*:* -brand:*)) and get what I want. But I can't find a way to formulate this or anything logically the same through haystack without really ugly hacks.
I did find this ugly hack:
SearchQuerySet().filter(brand=Raw('%s OR (*:* -brand:*)' % Clean('foo'))
But it chains really poorly with that OR in there without any parenthesis around it.
Ideally a solution using a pure filter would be best, but failing that a way to add a chainable filter using raw solr query language.
I'm using django-haystack 2.4.0
It's not a perfect match, but narrow helps me enough to let me do what I want
SearchQuerySet().narrow('(brand:%s OR (*:* -brand:*))' % Clean('foo'))

Should I use Connection::executeUpdate()?

I use Doctrine DBAL v2.5.0 and I would like to perform a simple update statement. In the documentation it is written that I should use the method executeUpdate() (http://doctrine-dbal.readthedocs.org/en/latest/reference/data-retrieval-and-manipulation.html#executeupdate) for that. But in the source code this method has the annotation #internal. Because of that I am not sure whether this method should be used from non-library code or not. Should I?
It seems that you have to use the executeUpdate() method on the doctrine service and not on the entity manager.
$this->container->get('doctrine.orm.entity_manager')->getConnection()->executeUpdate($query); gives a warning in my IDE that executeUpdate() is #internal.
$this->container->get('doctrine')->getConnection()->executeUpdate($query);
or in a controller $this->getDoctrine()->getConnection()->executeUpdate($query); does not give any warning.
With other words:
You want to call the executeUpdate() method on
\Doctrine\Bundle\DoctrineBundle\Registry class
instead of the
\Doctrine\ORM\EntityManager class
P.D. Maybe I should mention that I am using Doctrine in conjunction with Symfony2.

Best practice for implementing GET operation in Restful service

I am trying to find if there is any best practices involved in developing/implementing a GET operation.
I was going through the web resource documentation of jersey.
URL : http://jersey.java.net/nonav/apidocs/1.4/jersey/com/sun/jersey/api/client/WebResource.html
If we look at the methods that are available, the 'get' doesn't accept entity.
Is it recommended to implement get operation which doesnt accept entity but only get request parameters from Query Parameters?
Thanks,
GK
Yes. Think of the URI as the unique identifier to the object/resource you are GETing. I typically use query params for a GET if required. More normally I just have a GET something like this: GET: https:/myservice.com/myobject/id. This path is usually returned from a PUT or POST operation on MyObject. If I want to look up one or more I then use query params for the criteria.
There are a number of best practices out there. One that seems to bring together most of the common ones in a readable format is provided by Apigee. You can obtain it from http://info.apigee.com/Portals/62317/docs/web%20api.pdf

Can I use IN and findBy together in Groovy and Grails?

I've been trying to figure out how to do something like
SELECT * FROM domain WHERE config_id IN (1, 2, 3)
when using the nice findBy technique of Grails. Haven't really figured out how yet though, is it even possible? I was hoping for something like
Domain.findByConfigIn(configList)
but that doesn't work. Anyone knows how to do this, or even if it's possible?
Assuming your domain class has a field named configId, the following should do what you want:
Domain.findAllByConfigIdInList(configList)

Is there anything like ruby's .send in HTMLBars or Handlebars?

Was wondering if there's anything like ruby's .send for htmlbars. Been trying to search for it, but can't seem to find a proper answer. Here's what I want to do:
{{"name-of-helper" arguments}}
As simple as this. Is there any trick under it to achieve this effect?
There is no built-in one or supplemented one but if you need it you can write one for yourself.
Ember includes "component" helper which allows you to dynamically build component - it's not exactly the same, but might suit your needs.