I have the following query that I need to convert to Joomla using JDatabase
. This query executes as intended in the phpMyAdmin.
SELECT `id`, DATE_FORMAT(`estEventDate`, '%b %Y') AS estDate, COUNT(`id`) AS cnt FROM `jdev_events` GROUP BY estDate ORDER BY estDate DESC
I have tried many different ways, but with no luck.
The first problem I faced was that I cannot alias the function but only the field in the $db->quoteName()
.
$query->selcet(array($db->quoteName('id'), 'DATE_FORMAT(' . $db->quoteName('estEventDate') . ', "%b %Y")' . ' AS estDate', 'COUNT(' . $db->quoteName('id') . ') as cnt'));
But even if I remove the aliases...
$query->selcet(array($db->quoteName('id'), 'DATE_FORMAT(' . $db->quoteName('estEventDate') . ', "%b %Y")', 'COUNT(' . $db->quoteName('id') . ')'));
it does not work.
Then I tried to set the SELECT object as string :
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("`id`, DATE_FORMAT(`estEventDate`, '%b %Y') AS estDate, COUNT(`id`) AS cnt");
$query->from($db->quoteName('#__events'));
$query->group('estDate');
$query->order('estDate DESC');
$db->setQuery($query);
JLog::add('Query : ' . $query->__toString(), JLog::DEBUG, 'custom.field.estDate.getOptions');
$estDates = $db->loadObjectLsit();
JLog::add('EstDates' . print_r($estDates, 1), JLog::DEBUG, 'custom.field.estDate.getOptions');
Here are the log entries:
2018-11-06T00:37:36+00:00 DEBUG 127.0.0.1 custom.field.estdate.getOptions Query : SELECT `id`, DATE_FORMAT(`estEventDate`, '%b %Y') AS estDate, COUNT(`id`) AS cnt FROM `#__events` GROUP BY estDate ORDER BY estDate DESC 2018-11-06T00:37:36+00:00 DEBUG 127.0.0.1 custom.field.estdate.getOptions EstDates :
I am out of ideas.