Can I use whereIn in a query with named parameters? - php

I'm working on a Symfony app that uses Doctrine as the ORM. I want to run a query with an WHERE foo IN (bar) clause, and I'm adding the IN bit like so:
$query->andWhereIn('p.foo', $bar);
where $bar is an array of id numbers. Browsing the docs and trying out a few combinations, I was unable to make Doctrine treat the parameter I'm passing there as a named parameter.
As a result, I'm forced to use positional parameters for the rest of the query, too, as you can't mix the two. What, if anything, am I missing?

I wrote a patch for this http://www.doctrine-project.org/jira/browse/DC-1003?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel that was never committed but it does handle this scenario..

andWhereIn() is simply a proxy method for whereIn()
Have you tried to use whereIn() or did you skip directly to andWhereIn()?

Related

Doctrine Query builder - how to select based on field inside JSON column

I have a table with a JSON column, it's type longtext (DC2Type:json). This table has an Entity in Doctrine ORM in my Symfony project. I would like to query based on fields inside the JSON column, using the Doctrine query builder I have in a variable $qb
How do I do this? Everything I found online says to install a 3rd-party package to enable this. Is there no way to just do it with Doctrine's query builder without installing another package?
One (maybe dumb) workaround I tried was to treat the column as a string, and do...
$qb->andWhere("my_data LIKE \"%id:\\\"1,%\"");
For example, if I wanted to query the JSON column my_data to find the blobs that contain id":1, in the string. This fails with a very strange syntax error, and isn't the right way to query a JSON field anyway. HOWEVER, doing the LIKE query directly in SQL client works the way I want, so I also don't know why this is failing in Doctrine.
EDIT: This is MySQL / MariaDB.
Doctrine Query Language is pretty limited. It covers only the most basic/common SQL functions, which is enough for like 99% use cases, but not all.
If you have a MariaDB version that natively supports JSON (so 10.2 or later) you can use native functions to work with the JSON data. (If you don't then your workaround is the only option regardless, with perhaps some additional filtering in the application).
To be able to use these functions in DQL you either need to define them yourself or indeed use a third party library like scienta/doctrine-json-functions (note that it has documentation for how to use it with Symfony, and it's really simple).
If you need just a single extra function and for some reason don't want the whole bundle, you could just copy that single class and use it as your own.
Alternatively you can forgo DQL and write SQL directly, but that way you can't hydrate into objects directly and use other Doctrine magic with the data. But it can be enough for simple use cases.

In Symfony, how to get QueryBuilder from default findBy method?

In Symfony 5, using Doctrine, how can I get QueryBuilder object (instead of results) from default entity repository methods like findBy, findOneBy, findAll?
I need QueryBuilder for:
Passing it to KnpPaginator (requires specifically QueryBuilder instead of results)
Possibly extending it with additional query logic in the future
I could just write a simple query (like $em->createQuery("SELECT a FROM Article a")), but I want to have access to filtering and ordering provided by default findBy method. I think writing my own QueryBuilder with filtering/sorting by any property would be a lot of work and I'm not sure I could implement it well even if I tried.
EDIT: even though it does not answer my question exactly, I have found a solution for my 1st use case (using KnpPaginator without writing custom queries): Custom data repository pagination.
This method allows to attach pagination to any query without changing it instead of writing a new one through QueryBuilder.

Can you query a Sql Server View with PHP?

So I know that you can query a table using PHP as so:
$projectmanager=DB::table('Table_name')->distinct()->select('Table_Column')->get();
I want to know if you can do the same type of thing with a SQL Server View. I have tried the following:
$view=DB::view('View_Name')->select('View_Column')->get();
But I get the following error:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\SqlServerConnection' does not have a method 'view'
Are you using Laravel by chance? Whatever the framework, you are calling a method DB::view that doesn't exist, so some class magic fails.
To answer question, yes. To PHP they are seen the same as a regular table, treat it as such for querying (though you cannot manually update them of course).
$projectmanager=DB::table('View_name')->distinct()->select('View_column')->get();

Symfony2 Doctrine Querybuilder select all

I'm currently working on a Service in SF2 that queries the database with the QueryBuilder using a class variable set with a repository-specific QueryBuilder in the constructor of this Service.
Which means i would like to make use of this set QueryBuilder as much as possible for neater code and a clean feeling using it.
I want to avoid creating a query on the EntityManager, but instead solely query using this predefined Querybuilder.
I'm looking for something that would look/work like the following:
$query = $this->fooRepository->createQueryBuilder('f')->select('*');
return $query->getResult(Query::HYDRATE_ARRAY);
The above would (if it worked) return all the foo in the database as far as i know..
If you think i'm being stupid and should do something different in regard to the predefined QueryBuilders or just use the:
createQuery()
method because it simply isn't good practice or impossible, don't hesitate to tell me.
Thanks!
Try:
$qb = $this->fooRepository->createQueryBuilder('foo');
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
No need for a select(*). All the foo items will be selected since no where clauses were added.

PECL solr package issue

In solr PECL php package,to the solrQuery class we can add parameters using solrparam::set methods as name value pair.So Inorder to build a query we can use this SolrParams class.
I am just trying to figure out what are the use cases of all the methods in solrquery object.
like
***"SolrQuery::addFacetDateField — Maps to facet.date
SolrQuery::addFacetDateOther — Adds another facet.date.other parameter
SolrQuery::addFacetField — Adds another field to the facet
SolrQuery::addFacetQuery — Adds a facet query
SolrQuery::addField — Specifies which fields to return in the result
SolrQuery::addFilterQuery — Specifies a filter query
SolrQuery::addHighlightField — Maps to hl.fl"***
....etc.
We can simply use the solrparam to add parameters to the query, then what is the use of these.
Thanks
These methods were added to ease the use of Solr Functions, Unfortunately the current documentation misses a lot of use cases, that I'm currently working on.
It's much easier and more consistent to use methods for query functionalities like date facet without going into the Solr Documentation each time to pull parameter names. It's also less error prone.
For the time being, if you wish to learn more about these functionalities, you can check the Solr Wiki http://wiki.apache.org/solr/SimpleFacetParameters#facet.date.other
The SolrParams is the ancestor of SolrQuery, SolrQuery has much more features than the bare bones(SolrParams). In the documentation its passed for SolrClient::query() as the query method accepts argument of type SolrParams (which means SolrParams any of its descendents).
There is nothing special about those methods. You can use the API in both ways without any penalty. Using SolrQuery methods makes your code more explicit so it's better for readability I presume.

Categories