Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ->join method #119

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NotORM/MultiResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function aggregation($function) {
}
}

function count($column = "") {
function count($column = ""):int {
$return = parent::count($column);
return (isset($return) ? $return : 0);
}
Expand Down
42 changes: 27 additions & 15 deletions NotORM/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
class NotORM_Result extends NotORM_Abstract implements Iterator, ArrayAccess, Countable, JsonSerializable {
protected $single;
protected $select = array(), $conditions = array(), $where = array(), $parameters = array(), $order = array(), $limit = null, $offset = null, $group = "", $having = "", $lock = null;
protected $select = array(), $conditions = array(), $where = array(), $parameters = array(), $order = array(), $limit = null, $offset = null, $group = "", $having = "", $lock = null, $join = array();
protected $union = array(), $unionOrder = array(), $unionLimit = null, $unionOffset = null;
protected $data, $referencing = array(), $aggregation = array(), $accessed, $access, $keys = array();

Expand Down Expand Up @@ -104,6 +104,11 @@ protected function createJoins($val) {
}
}
}
if(count($this->join)) {
foreach ($this->join as $tableAlias => $joinStartament) {
$return[$tableAlias]=$joinStartament;
}
}
return $return;
}

Expand Down Expand Up @@ -392,6 +397,13 @@ function where($condition, $parameters = array()) {
return $this->whereOperator("AND", $args);
}

function join($table,$on=false,$outer=false) {
$tbl=preg_replace('/^[^a-z]*((?:.* as )?`?([a-z][a-z0-9\_]*)).*$/i','$2',$table);
$this->join[$tbl]=" LEFT ".($outer?"OUTER ":"")."JOIN ".$table." ".($on===false?"":" ON ".$on);

return $this;
}

protected function whereOperator($operator, array $args) {
$condition = $args[0];
$parameters = (count($args) > 1 ? $args[1] : array());
Expand Down Expand Up @@ -587,7 +599,7 @@ function aggregation($function) {
* @param string
* @return int
*/
function count($column = "") {
function count($column = ""): int {
if (!$column) {
$this->execute();
return count($this->data);
Expand All @@ -599,23 +611,23 @@ function count($column = "") {
* @param string
* @return int
*/
function min($column) {
function min($column):int {
return $this->aggregation("MIN($column)");
}

/** Return maximum value from a column
* @param string
* @return int
*/
function max($column) {
function max($column):int {
return $this->aggregation("MAX($column)");
}

/** Return sum of values in a column
* @param string
* @return int
*/
function sum($column) {
function sum($column):int {
return $this->aggregation("SUM($column)");
}

Expand Down Expand Up @@ -729,27 +741,27 @@ protected function single() {

// Iterator implementation (not IteratorAggregate because $this->data can be changed during iteration)

function rewind() {
function rewind():void {
$this->execute();
$this->keys = array_keys($this->data);
reset($this->keys);
}

/** @return NotORM_Row */
function current() {
function current():NotORM_Row {
return $this->data[current($this->keys)];
}

/** @return string row ID */
function key() {
function key(): string {
return current($this->keys);
}

function next() {
function next(): void {
next($this->keys);
}

function valid() {
function valid(): bool {
return current($this->keys) !== false;
}

Expand All @@ -759,7 +771,7 @@ function valid() {
* @param string row ID or array for where conditions
* @return bool
*/
function offsetExists($key) {
function offsetExists($key): bool {
$row = $this->offsetGet($key);
return isset($row);
}
Expand All @@ -768,7 +780,7 @@ function offsetExists($key) {
* @param string row ID or array for where conditions
* @return NotORM_Row or null if there is no such row
*/
function offsetGet($key) {
function offsetGet($key):NotORM_Row|null {
if ($this->single && !isset($this->data)) {
$clone = clone $this;
if (is_array($key)) {
Expand Down Expand Up @@ -802,7 +814,7 @@ function offsetGet($key) {
* @param NotORM_Row
* @return null
*/
function offsetSet($key, $value) {
function offsetSet($key, $value): void {
$this->execute();
$this->data[$key] = $value;
}
Expand All @@ -811,14 +823,14 @@ function offsetSet($key, $value) {
* @param string row ID
* @return null
*/
function offsetUnset($key) {
function offsetUnset($key): void {
$this->execute();
unset($this->data[$key]);
}

// JsonSerializable implementation

function jsonSerialize() {
function jsonSerialize():JsonSerializable {
$this->execute();
if ($this->notORM->jsonAsArray) {
return array_values($this->data);
Expand Down
14 changes: 7 additions & 7 deletions NotORM/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ protected function access($key, $delete = false) {

// IteratorAggregate implementation

function getIterator() {
function getIterator(): IteratorAggregate {
$this->access(null);
return new ArrayIterator($this->row);
}

// Countable implementation

function count() {
function count(): int {
return count($this->row);
}

Expand All @@ -145,7 +145,7 @@ function count() {
* @param string column name
* @return bool
*/
function offsetExists($key) {
function offsetExists($key): bool {
$this->access($key);
$return = array_key_exists($key, $this->row);
if (!$return) {
Expand All @@ -158,7 +158,7 @@ function offsetExists($key) {
* @param string column name
* @return string
*/
function offsetGet($key) {
function offsetGet($key): string {
$this->access($key);
if (!array_key_exists($key, $this->row)) {
$this->access($key, true);
Expand All @@ -170,7 +170,7 @@ function offsetGet($key) {
* @param string column name
* @return null
*/
function offsetSet($key, $value) {
function offsetSet($key, $value): void {
$this->row[$key] = $value;
$this->modified[$key] = $value;
}
Expand All @@ -179,14 +179,14 @@ function offsetSet($key, $value) {
* @param string column name
* @return null
*/
function offsetUnset($key) {
function offsetUnset($key): void {
unset($this->row[$key]);
unset($this->modified[$key]);
}

// JsonSerializable implementation

function jsonSerialize() {
function jsonSerialize(): JsonSerializable {
return $this->row;
}

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vrana/notorm",
"description": "NotORM is a PHP library for simple working with data in the database.",
"name": "trijin/notorm",
"description": "NotORM is a PHP library for simple working with data in the database. + with advanced join",
"keywords": ["database", "dbal"],
"homepage": "http://www.notorm.com/",
"license": ["Apache-2.0", "GPL-2.0+"],
Expand All @@ -9,7 +9,15 @@
"name": "Jakub Vrána",
"homepage": "http://www.vrana.cz/"
}
,{
"name": "Alex T Trijin",
"homepage": "http://trijin.ru/",
"role": "Add `join` comand"
}
],
"replace": {
"vrana/notorm": "dev-master"
},
"autoload": {
"files": ["NotORM.php"]
}
Expand Down
6 changes: 6 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PHP 5.1+
any database supported by PDO (tested with MySQL, SQLite, PostgreSQL, MS SQL, Oracle)

Usage:

<?php
include "NotORM.php";
$connection = new PDO("mysql:dbname=software");
Expand All @@ -20,3 +21,8 @@ foreach ($software->application()->order("title") as $application) { // get all
}
}
?>

In this version you can something like this:
$db->books
->select('books.id, books.title, books2.id as id2, books2.title as title2')
->join('books as books2','books.author=books2.author AND books.id!=books2.id');