I will try explain the new JQuery Class from 1.6 ;)
The idea of JQuery Class is create dynamic SQL on execution time. Very simple to use.
// Create a new query object.
$query = new JQuery();
$query->select("id, title, alias");
$query->from('#__content');
echo $query; //return something like SELECT id, title, alias FROM #__content
more complex querys:
$query = new JQuery();
$query->select("c.id, c.title, c.alias");
$query->from('#__content as c');
$query->where('c.access = 1');
$query->order('c.created DESC');
echo $query; //return SELECT c.id, c.title, c.alias FOM #__content as c WHERE c.access = 1 ORDER c.created DESC
Now I will show a little secret. How tu use in DSL(Domain Specific Language).
If you think in future versions all users/developers use the same language to comunicate.
We can construct a Catalogs for construct Dyamic Querys.. I will show some examples that use Fluent Interface + DSLs + JQuery Class;
I will introduce another idea called DSLAPI(Domain Specific Language API) you can have DSLs from applications like Joomla!, Wordpress, Phpbb, Drupal, etc..
First we focus on Joomla! let go to contruct the first DSL class.
/**
* Catalog Content class
*
* @package DSL
* @since 1.0
*/
class DSL_Content
{
/**
* create a instance of JQuery
*
* @return Object JQuery
*/
protected function select() {
if( !$this->_query ){
$this->_query = new JQuery();
}
return $this->_query;
}
/**
* Initialize select with a list
*/
public function lists() {
$this->select()->select("
#__content.id,
#__content.title,
#__content.alias
");
$this->select()->from('#__content');
return $this;
}
/**
* Initialize select with count
*/
public function count() {
$this->select()->select(" COUNT(#__content.id) ");
$this->select()->from('#__content');
return $this;
}
/**
* Filter content from specific state
*
* @param $stateCondition Integer Sate of Content
*/
public function withState($stateCondition) {
$this->select()->where("#__content.state = {$stateCondition}");
return $this;
}
/**
* Filter content from specific parent id
*
* @param Integer Sate of Content
*/
public function withParendId($parendId = 0) {
$this->select()->where("#__content.parentid = {$parendId}");
return $this;
}
/**
* Filter content with specific title
*
* @param String title of content
*/
public function withTitle($title) {
$this->select()->where("#__content.title = '{$title}'");
return $this;
}
/**
* Filter content with specific title like
*
* @param String title of content
*/
public function withTitleLike($title) {
$this->select()->where("#__content.title LIKE '%{$title}%'");
return $this;
}
/**
* Order content by creation date
*
*/
public function latest() {
$this->select()->order("#__content.created DESC");
return $this;
}
/**
* Filter content by hits
*
* @param Integer Number of hits
*/
public function withHitsEqualThan($number) {
$this->select()->where("#__content.hits = {$number}");
return $this;
}
/**
* Filter content with hits more than number
*
* @param Integer Number of hits
*/
public function withHitsMoreThan($number) {
$this->select()->where("#__content.hits > {$number}");
return $this;
}
/**
* Filter content with hits less than number
*
* @param Integer Number of hits
*/
public function withHitsLessThan($number) {
$this->select()->where("#__content.hits < {$number}");
return $this;
}
/**
* Filter content in frontpage
*
* @param Integer Sate of Content
*/
public function inFrontpage() {
$this->select()->JOIN('JOIN',"#__content_frontpage ON (#__content.id = #__content_frontpage.content_id)");
return $this;
}
public function getRows(){
$db = JFactory::getDBO();
$db->setQuery( $this->select()->__toString() );
return $db->loadObjectList()
}
public function getResult(){
$db = JFactory::getDBO();
$db->setQuery( $this->select()->__toString() );
return $db->loadResult()
}
}
Now you can use something like:
$content = new DSL_Content();
$rowsContentFrontpage = $content->lists()->latest()->inFrontpage()->getRows();
//if you need count is just
$total = $content->count()->getResult();
Why I not re-config with same setings taht use on select? Because we just set before.
first code you get lists latest content in frontpage
secont get total.
Soon I will write more and more..
Tags:
Share
Facebook
You need to be a member of All Together, As A Whole to add comments!
Join All Together, As A Whole