From fb6e99326ffc910d1f27dc818bc208a9c1040b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Can=20Karag=C3=B6z?= <32842570+Smokietr@users.noreply.github.com> Date: Tue, 14 Nov 2017 06:04:55 +0300 Subject: [PATCH 1/2] Update BasicDB Random function added. --- src/BasicDB.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/BasicDB.php b/src/BasicDB.php index 238a290..8a859e8 100644 --- a/src/BasicDB.php +++ b/src/BasicDB.php @@ -49,6 +49,13 @@ class BasicDB extends \PDO * @var * */ + private $rand; + /** + * OrderBy Rand() + * + * @var + * + */ private $orderBy; /** * GroupBy Value @@ -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 @@ -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; From e45ebeab41c56c1230e5dc9aa289884e8b9d1ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Can=20Karag=C3=B6z?= <32842570+Smokietr@users.noreply.github.com> Date: Tue, 14 Nov 2017 06:08:31 +0300 Subject: [PATCH 2/2] Update all.php --- examples/all.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/all.php b/examples/all.php index 81a24b1..7f848f9 100644 --- a/examples/all.php +++ b/examples/all.php @@ -44,6 +44,11 @@ ->groupby('column_name') ->run(); +//random +$db->from('table_name') + ->rand() + ->run(); + // limit $db->from('table_name') ->limit(0, 20)