Skip to content

Commit

Permalink
Merge pull request #100 from g9308370/add_optional_orderby
Browse files Browse the repository at this point in the history
add a new optional input  "orderby"
  • Loading branch information
tihomiro committed Jul 13, 2017
2 parents 40578a0 + 839c034 commit 4d635d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function readStubFile($file)
* @return bool
* @throws Orangehill\Iseed\TableNotFoundException
*/
public function generateSeed($table, $database = null, $max = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true)
public function generateSeed($table, $database = null, $max = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC')
{
if (!$database) {
$database = config('database.default');
Expand All @@ -73,7 +73,7 @@ public function generateSeed($table, $database = null, $max = 0, $exclude = null
}

// Get the data
$data = $this->getData($table, $max, $exclude);
$data = $this->getData($table, $max, $exclude, $orderBy, $direction);

// Repack the data
$dataArray = $this->repackSeedData($data);
Expand Down Expand Up @@ -128,7 +128,7 @@ public function getSeedPath()
* @param string $table
* @return Array
*/
public function getData($table, $max, $exclude = null)
public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC')
{
$result = \DB::connection($this->databaseName)->table($table);

Expand All @@ -137,6 +137,10 @@ public function getData($table, $max, $exclude = null)
$result = $result->select(array_diff($allColumns, $exclude));
}

if($orderBy) {
$result = $result->orderBy($orderBy, $direction);
}

if ($max) {
$result = $result->limit($max);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Orangehill/Iseed/IseedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function fire()
$postrunEvents = explode(",", $this->option('postrun'));
$dumpAuto = intval($this->option('dumpauto'));
$indexed = !$this->option('noindex');
$orderBy = $this->option('orderby');
$direction = $this->option('direction');

if ($chunkSize < 1) {
$chunkSize = null;
Expand Down Expand Up @@ -83,7 +85,9 @@ public function fire()
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed
$indexed,
$orderBy,
$direction
),
$table
);
Expand Down Expand Up @@ -140,6 +144,8 @@ protected function getOptions()
array('postrun', null, InputOption::VALUE_OPTIONAL, 'postrun event name', null),
array('dumpauto', null, InputOption::VALUE_OPTIONAL, 'run composer dump-autoload', true),
array('noindex', null, InputOption::VALUE_NONE, 'no indexing in the seed', null),
array('orderby', null, InputOption::VALUE_OPTIONAL, 'orderby desc by column', null),
array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null),
);
}

Expand Down

0 comments on commit 4d635d2

Please sign in to comment.