API16:JDatabaseQuery
(New page: <span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki>
</span>
{{Description:JDatabaseQuery}}
===Define...) |
(→Examples) |
||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 64: | Line 64: | ||
|} | |} | ||
| + | |||
===Importing=== | ===Importing=== | ||
<source lang="php">jimport( 'joomla.database.databasequery' );</source> | <source lang="php">jimport( 'joomla.database.databasequery' );</source> | ||
| Line 82: | Line 83: | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
| + | '''Simple Example''' | ||
| + | |||
| + | The following is a very simple use of the JDatabaseQuery class: | ||
| + | <source lang="php"> | ||
| + | function getListQuery() | ||
| + | { | ||
| + | $query = new JDatabaseQuery(); | ||
| + | $query->select('*'); | ||
| + | $query->from('#__categories'); | ||
| + | return $query; | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | ---- | ||
| + | '''Complex Example''' | ||
| + | |||
| + | Consider a case in which we have three tables called "property", "building" and "unit". The primary keys for these tables are "property_id", "building_id" and "unit_id", respectively. We want to build a method which returns the number of units associated with a property_id which is passed to our method. The unit table has no direct relationship to the property table. It does have a relationship to the building table, which in turn has a relationship to the property table. The following method will build the query and execute it, returning the value for which we're looking. | ||
| + | <source lang="php"> | ||
| + | function countUnits($property_id) | ||
| + | { | ||
| + | $db =& JFactory::getDBO(); | ||
| + | $query = $db->getQuery(true); | ||
| + | $query->select('count(*)'); | ||
| + | $query->from('#__pt_building AS b'); | ||
| + | $query->leftJoin('#__pt_unit AS u ON b.building_id = u.building_id'); | ||
| + | $query->where('b.property_id = '. (int) $property_id); | ||
| + | $db->setQuery($query); | ||
| + | return $db->loadResult(); | ||
| + | } | ||
| + | </source> | ||
Latest revision as of 19:36, 7 March 2011
[Edit Descripton] Description:JDatabaseQuery
Contents |
[edit] Defined in
libraries/joomla/database/databasequery.php
[edit] Methods
| Method name | Description |
|---|---|
| clear | Clear data from the query or a specific clause of the query. |
| select | |
| delete | |
| insert | |
| update | |
| from | |
| join | |
| innerJoin | |
| outerJoin | |
| leftJoin | |
| rightJoin | |
| set | |
| where | |
| group | |
| having | |
| order | |
| __toString | string The completed query |
[edit] Importing
jimport( 'joomla.database.databasequery' );
[Edit See Also] SeeAlso:JDatabaseQuery
[edit] Examples
<CodeExamplesForm />
Simple Example
The following is a very simple use of the JDatabaseQuery class:
function getListQuery() { $query = new JDatabaseQuery(); $query->select('*'); $query->from('#__categories'); return $query; }
Complex Example
Consider a case in which we have three tables called "property", "building" and "unit". The primary keys for these tables are "property_id", "building_id" and "unit_id", respectively. We want to build a method which returns the number of units associated with a property_id which is passed to our method. The unit table has no direct relationship to the property table. It does have a relationship to the building table, which in turn has a relationship to the property table. The following method will build the query and execute it, returning the value for which we're looking.
function countUnits($property_id) { $db =& JFactory::getDBO(); $query = $db->getQuery(true); $query->select('count(*)'); $query->from('#__pt_building AS b'); $query->leftJoin('#__pt_unit AS u ON b.building_id = u.building_id'); $query->where('b.property_id = '. (int) $property_id); $db->setQuery($query); return $db->loadResult(); }
