Commit 65b68b2d authored by Alexander Butenko's avatar Alexander Butenko

Added JOIN method

parent 9bf6ab7d
...@@ -30,6 +30,12 @@ class MysqliDb ...@@ -30,6 +30,12 @@ class MysqliDb
* @var string * @var string
*/ */
protected $_query; protected $_query;
/**
* An array that holds where joins
*
* @var array
*/
protected $_join = array();
/** /**
* An array that holds where conditions 'fieldname' => 'value' * An array that holds where conditions 'fieldname' => 'value'
* *
...@@ -105,7 +111,9 @@ class MysqliDb ...@@ -105,7 +111,9 @@ class MysqliDb
protected function reset() protected function reset()
{ {
$this->_where = array(); $this->_where = array();
$this->_orderBy = array(); $this->_join = array();
$this->_orderBy = array();
$this->groupBy = array();
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
unset($this->_query); unset($this->_query);
unset($this->_whereTypeList); unset($this->_whereTypeList);
...@@ -261,6 +269,29 @@ class MysqliDb ...@@ -261,6 +269,29 @@ class MysqliDb
return $this; return $this;
} }
/**
* This method allows you to concatenates joins for the final SQL statement. Simple as a pimple.
*
* @uses $MySqliDb->join('table1', 'field1 <> field2', 'LEFT')
*
* @param string $joinTable The name of the table.
* @param string $joinCondition the condition.
* @param string $joinType 'LEFT', 'INNER' etc.
*
* @return MysqliDb
*/
public function join($joinTable, $joinCondition, $joinType = '')
{
$allowedTypes = array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER');
if ($joinType && in_array ($joinType, $allowedTypes))
$joinType = strtoupper (trim ($joinType));
else
$joinType = '';
$this->_join[$joinType . " JOIN " . $joinTable] = $joinCondition;
return $this;
}
/** /**
* This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries. * This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.
* *
...@@ -362,6 +393,13 @@ class MysqliDb ...@@ -362,6 +393,13 @@ class MysqliDb
$hasTableData = is_array($tableData); $hasTableData = is_array($tableData);
$hasConditional = !empty($this->_where); $hasConditional = !empty($this->_where);
// Did the user call the "join" method?
if (!empty($this->_join)) {
foreach ($this->_join as $prop => $value) {
$this->_query .= " " . $prop . " on " . $value;
}
}
// Did the user call the "where" method? // Did the user call the "where" method?
if (!empty($this->_where)) { if (!empty($this->_where)) {
......
...@@ -131,3 +131,9 @@ $results = $db->get('tableName'); ...@@ -131,3 +131,9 @@ $results = $db->get('tableName');
$db->groupBy("name"); $db->groupBy("name");
$results = $db->get('tableName'); $results = $db->get('tableName');
``` ```
### JOIN method
```php
$db->join('table2Name', 'field1 <> field2', 'LEFT')
$results = $db-get('tableName');
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment