Skip to content

Commit

Permalink
optimized query in the getOptions method
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeeshaan committed Jun 7, 2014
1 parent 62dbc63 commit c6ef6f9
Showing 1 changed file with 29 additions and 27 deletions.
Expand Up @@ -63,56 +63,58 @@ protected function getOptions()

$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('a.id AS value, a.title AS text, a.level, a.published')
->from('#__categories AS a')
->join('LEFT', $db->quoteName('#__categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
->select('DISTINCT a.id AS value, a.title AS text, a.level, a.published');
$subQuery = $db->getQuery(true)
->select('id,title,level,published,parent_id,extension,lft,rgt')
->from('#__categories');

// Filter by the extension type
if ($this->element['parent'] == true || $jinput->get('option') == 'com_categories')
{
$query->where('(a.extension = ' . $db->quote($extension) . ' OR a.parent_id = 0)');
$subQuery->where('(extension = ' . $db->quote($extension) . ' OR parent_id = 0)');
}
else
{
$query->where('(a.extension = ' . $db->quote($extension) . ')');
}
// If parent isn't explicitly stated but we are in com_categories assume we want parents
if ($oldCat != 0 && ($this->element['parent'] == true || $jinput->get('option') == 'com_categories'))
{
// Prevent parenting to children of this item.
// To rearrange parents and children move the children up, not the parents down.
$query->join('LEFT', $db->quoteName('#__categories') . ' AS p ON p.id = ' . (int) $oldCat)
->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');

$rowQuery = $db->getQuery(true);
$rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id')
->from('#__categories AS a')
->where('a.id = ' . (int) $oldCat);
$db->setQuery($rowQuery);
$row = $db->loadObject();
$subQuery->where('(extension = ' . $db->quote($extension) . ')');
}

// Filter language
if (!empty($this->element['language']))
{

$query->where('a.language = ' . $db->quote($this->element['language']));
$subQuery->where('language = ' . $db->quote($this->element['language']));
}

// Filter on the published state

if (is_numeric($published))
{
$query->where('a.published = ' . (int) $published);
$subQuery->where('published = ' . (int) $published);
}
elseif (is_array($published))
{
JArrayHelper::toInteger($published);
$query->where('a.published IN (' . implode(',', $published) . ')');
$subQuery->where('published IN (' . implode(',', $published) . ')');
}

$query->group('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id, a.published')
->order('a.lft ASC');
$subQuery->group('id, title, level, lft, rgt, extension, parent_id,published')
->order('lft ASC');
$query->from('(' . $subQuery->__toString() . ') AS a')
->join('LEFT', $db->quoteName('#__categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');

// If parent isn't explicitly stated but we are in com_categories assume we want parents
if ($oldCat != 0 && ($this->element['parent'] == true || $jinput->get('option') == 'com_categories'))
{
// Prevent parenting to children of this item.
// To rearrange parents and children move the children up, not the parents down.
$query->join('LEFT', $db->quoteName('#__categories') . ' AS p ON p.id = ' . (int) $oldCat)
->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');

$rowQuery = $db->getQuery(true);
$rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id')
->from('#__categories AS a')
->where('a.id = ' . (int) $oldCat);
$db->setQuery($rowQuery);
$row = $db->loadObject();
}

// Get the options.
$db->setQuery($query);
Expand Down

0 comments on commit c6ef6f9

Please sign in to comment.