Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Forked from gunjanpatel/jdatabase.md
Created April 22, 2014 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betweenbrain/11169049 to your computer and use it in GitHub Desktop.
Save betweenbrain/11169049 to your computer and use it in GitHub Desktop.

Here is an example of write subquery in Joomla! 3 using JDatabase method.

<?php
// Initialize variables.
$db       = JFactory::getDbo();
$subQuery = $db->getQuery(true);
$query    = $db->getQuery(true);

// Create the base subQuery select statement.
$subQuery->select('*')
	->from($db->quoteName('#__sub_table'))
	->where($db->quoteName('subTest') . ' = ' . $db->quote('1'));

// Create the base select statement.
$query->select('*')
	->from($db->quoteName('#__table'))
	->where($db->quoteName('state') . ' = ' . $db->quote('1'))
	->where($db->quoteName('subCheckIn') . ' IN (' . $subQuery->__toString() . ')')
	->order($db->quoteName('ordering') . ' ASC');

// Set the query and load the result.
$db->setQuery($query);

try
{
	$result = $db->loadObjectList();
}
catch (RuntimeException $e)
{
	throw new RuntimeException($e->getMessage(), $e->getCode());
}

By 6|_||\|74|\| |>4731

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment