I'm trying to use convert_tz for order by my query. When I run it on mysql it works perfect but when I use it with the ORM of my application It does not work. There's a right way to write it on my code?
Here is my actual code:
$qb->select('table1');
$qb->from('Entities\Table1', 'table1');
$qb->orderBy("CONVERT_TZ(concat(table1.date, ' ', table1.hour), table1.timezone, 'America/Sao_Paulo')", "asc");
$qb->getQuery()->getResult();
That's a MySQL-specific function which isn't defined in DQL.
If you must use it then you'll have to use native queries or install something like the DoctrineExtensions library, which claims to add support for CONVERT_TZ().
Related
I had the need to include in one of my system's DQL queries, a subquery with LIMIT clause. As Doctrine doesn't support it, I changed it to a native query. Yet the native query is returning lower case fields instead of the correct case.
The case is that as this is working code, I had some views depending on this names and it's much harder to change all names.
But I found here http://bit.ly/1Ht1ojH, that this aspect can be configured in Doctrine. So I tried this code:
$conn = $this->getConnection();
$conn->setAttribute(Doctrine_Core::ATTR_FIELD_CASE, CASE_NATURAL);
$res = $conn->query("select MyCasedField from whatever")->fetchAll();
Yet I'm getting the error "Attempted to call method "setAttribute" on class "Doctrine\ORM\EntityManager".
I tried with manager also with same result.
Now I now I can make some code to translate the fields, but I find that the configure solution to be much more clean.
Someone knows why symfony doesn't let me configure the connection ?
Also if there is any way of using LIMIT in a DQL's subquery I would find it better.
There is no LIMIT keyword in DQL. To use this functionality you can call method setMaxResults($limit) on your query object. It is also can be applied to Query Builder.
$query = $this->getEntityManager()
->createQuery('SELECT p FROM Product p')
->setMaxResults($limit);
I want to count my results of an active record query with CI (using postgreSQL). I am using count_all_results(), which works fine for most queries, but not when using distinct() or group_by() after a join, because count_all_results() ignores these functions.
Here is a fix vor this, which is not yet implemented in the newsest (2.1.3) stable version.
https://github.com/EllisLab/CodeIgniter/commit/b05f506daba5dc954fc8bcae76b4a5f97a7433c1
When I try to implement this fix in the current version myself, there is no additional filtering done. The row count stays the same.
Any tips on how to implement this in the current version, or other ways to count results filtered by distinct() or group_by()?
$this->db->select('COUNT(id)');
$this->db->join();
$this->db->distinct();
$this->db->group_by();
//...etc ...
$query = $this->db->get('mytable');
return count($query->result());
or
$this->db->join();
$this->db->distinct();
$this->db->group_by();
//...etc ...
$query = $this->db->get('mytable');
return $query->num_rows();
You can use
$this->db->group_by();
otherwise
$this->db->distinct();
In my case, using Codeigniter 3.1.11 as of today, distinct() is taken into account when doing count_all_results(). HOWEVER you have to use the function, doing a $this->db->select("distinct x") and then the counting, won't apply the DISTINCT limitation for the record count.
I am newbie on Laravel, I have a proper SQL Statement (which works on PHPMyAdmin and Navicat) and I can get results. What i want to do is, I want to take that statements in Laravel without using DB::raw();.
Any Helps will be appreciated.
Select
rmm.message,
count(rmm.message) as number,
receivedTime as time
FROM
rcs rcs, rmm rmm
WHERE
rmm.smsCid = rcs.smsCid AND rmm.receivedTime LIKE '%2013-04-01%' AND length('rmm.message') > '3'
GROUP BY(rmm.message)
Alternatively you can use the Fluent Query Builder. Here's my attempt, of course it's not checked as I don't have your data structure to hand. Hopefully enough to get you started:
DB::table('rcs') // SELECT FROM rcs
->join('rmm', 'rmm.smsCid', '=', 'rcs.smsCid') // Simple INNER join
->where('rmm.receivedTime', 'LIKE', '%2013-04-01%')
->where(DB::raw('LENGTH(rmm.message)'), '>', 3)
->get(array(DB::raw('COUNT(rmm.message)'), 'rmm.message', 'receivedTime')); // Get only the columns you want
You can use DB::query() to run your query. This calls the query method on the active DB connection.
Using Symfony2 and Doctrine2, want to combine CONCAT_WS with IN, rather than write a lot of IF's.
WHERE CONCAT_WS('-', id2, id2) IN ($ids)
I am receiving this error:
[Syntax Error] line 0, col 446: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_IN, got '('
$ids are in form of '123-456'.
If I simply use a column instead of CONCAT_WS, it works:
WHERE id2 IN ($ids)
Not all MySQL functions are available for use in Doctrine. Only those functions that are common to all databases supported are available.
Follow this link to see the list of functions available by default http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#functions-operators-aggregates .
If you want to use CONCAT_WS anyway, read about creating custom functions on the following links. You can create your own DQL Custom Function and will be able to use just like you tried before.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html
http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html
I've had the same problem as this, I solved it using this:
CONCAT(CONCAT('-', id2), id2)
Not the most elegant solution but it works. Writing a custom function is the way to go.
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('MyBundle:Listing')
->select('title')
->field('coordinates')->geoNear(
(float)$longitude,
(float)$latitude
)->spherical(true);
$classifieds_array = $classifieds->toArray();
$data = array('success'=>true,'classifieds' => $classifieds_array,
'displaymessage' => $classifieds->count(). " Search Results Found");
Even though I am selecting just one field, for my result set, I am getting every thing back in collection along with title. Is this a bug?
NOTE: I commented out the ->field('coordinates')->geoNear((float)$longitude, (float)$latitude)->spherical(true) line and now the select seems to work. This is crazy.
The geoNear command in MongoDB doesn't seem to support filtering result fields, according to the documentation examples. Only a query option is supported to limit matched documents.
In your case, it also looks like mixing up the geoNear() and near() builder methods in Doctrine. Since you're operating on the coordinates field, the appropriate syntax would be near(). geoNear() is a top-level method to tell the builder you wish to use the command, which doesn't require a field name since it uses the one and only geospatial index on the collection.
For usage examples, I would advise looking at the query and builder unit tests in the Doctrine MongoDB library.