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

Added JOIN method

parent 9bf6ab7d
......@@ -30,6 +30,12 @@ class MysqliDb
* @var string
*/
protected $_query;
/**
* An array that holds where joins
*
* @var array
*/
protected $_join = array();
/**
* An array that holds where conditions 'fieldname' => 'value'
*
......@@ -105,7 +111,9 @@ class MysqliDb
protected function reset()
{
$this->_where = array();
$this->_join = array();
$this->_orderBy = array();
$this->groupBy = array();
$this->_bindParams = array(''); // Create the empty 0 index
unset($this->_query);
unset($this->_whereTypeList);
......@@ -261,6 +269,29 @@ class MysqliDb
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.
*
......@@ -362,6 +393,13 @@ class MysqliDb
$hasTableData = is_array($tableData);
$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?
if (!empty($this->_where)) {
......
......@@ -131,3 +131,9 @@ $results = $db->get('tableName');
$db->groupBy("name");
$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