Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.
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
5 changes: 5 additions & 0 deletions examples/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
->groupby('column_name')
->run();

//random
$db->from('table_name')
->rand()
->run();

// limit
$db->from('table_name')
->limit(0, 20)
Expand Down
25 changes: 25 additions & 0 deletions src/BasicDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class BasicDB extends \PDO
* @var
*
*/
private $rand;
/**
* OrderBy Rand()
*
* @var
*
*/
private $orderBy;
/**
* GroupBy Value
Expand Down Expand Up @@ -201,6 +208,20 @@ public function join($targetTable, $joinSql, $joinType = 'inner')
$this->join[] = ' ' . strtoupper($joinType) . ' JOIN ' . $targetTable . ' ON ' . sprintf($joinSql, $targetTable, $this->tableName);
return $this;
}

/**
* Defines random operation in sql query
*
* @param
* $limit
* Default
* 5
* @return $this
*/
public function rand($limit = 5){
$this->rand = ' ORDER BY rand() LIMIT ' .$limit;
return $this;
}

/**
* Defines -orderby- operation in sql query
Expand Down Expand Up @@ -276,6 +297,10 @@ public function generateQuery()
$this->sql .= $this->orderBy;
$this->orderBy = null;
}
if ($this->rand) {
$this->sql .= $this->rand;
$this->orderBy = null;
}
if ($this->limit) {
$this->sql .= $this->limit;
$this->limit = null;
Expand Down