From 6e2b7f29ad42cd6ea5660c00cb53aa399f5fee92 Mon Sep 17 00:00:00 2001 From: Patrick Hill Date: Mon, 7 Mar 2022 17:02:47 +0100 Subject: [PATCH 1/2] adding where option --- README.md | 10 ++++++++++ src/Orangehill/Iseed/Iseed.php | 10 +++++++--- src/Orangehill/Iseed/IseedCommand.php | 10 ++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 110491f..31eae71 100755 --- a/README.md +++ b/README.md @@ -132,6 +132,16 @@ Example: artisan iseed users --max=10 --orderby=id --direction=desc ``` +### where +Optional parameter which allows you to add a raw sql-condition to the query + +Example: +``` +artisan iseed users --where="id BETWEEN 1 AND 100" +artisan iseed users --where="typeId in (1,2,4)" +artisan iseed users --where="deletedAt IS NULL" +``` + ### exclude Optional parameter which accepts comma separated list of columns that you'd like to exclude from tables that are being exported. In case of multiple tables, exclusion will be applied to all of them. diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index 20e5c8e..246b333 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -61,7 +61,7 @@ public function readStubFile($file) * @return bool * @throws Orangehill\Iseed\TableNotFoundException */ - public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC') + public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', $where = null) { if (!$database) { $database = config('database.default'); @@ -75,7 +75,7 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul } // Get the data - $data = $this->getData($table, $max, $exclude, $orderBy, $direction); + $data = $this->getData($table, $max, $exclude, $orderBy, $direction, $where); // Repack the data $dataArray = $this->repackSeedData($data); @@ -130,7 +130,7 @@ public function getSeedPath() * @param string $table * @return Array */ - public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC') + public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC', $where = null) { $result = \DB::connection($this->databaseName)->table($table); @@ -147,6 +147,10 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi $result = $result->limit($max); } + if ($where) { + $result = $result->whereRaw($where); + } + return $result->get(); } diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php index 58517ec..dc2848f 100755 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -66,6 +66,7 @@ public function fire() $direction = $this->option('direction'); $prefix = $this->option('classnameprefix'); $suffix = $this->option('classnamesuffix'); + $where = $this->option('where'); if ($max < 1) { $max = null; @@ -107,7 +108,8 @@ public function fire() $dumpAuto, $indexed, $orderBy, - $direction + $direction, + $where ), $table ); @@ -128,7 +130,10 @@ public function fire() $prerunEvent, $postrunEvent, $dumpAuto, - $indexed + $indexed, + $orderBy, + $direction, + $where ), $table ); @@ -172,6 +177,7 @@ protected function getOptions() array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null), array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null), array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null), + array('where', null, InputOption::VALUE_OPTIONAL, 'raw sql where condition', null), ); } From a99505b53c22b1254bea77ada3170c51cab16ed8 Mon Sep 17 00:00:00 2001 From: Patrick Hill Date: Tue, 8 Mar 2022 13:53:52 +0100 Subject: [PATCH 2/2] composer package name --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b20240a..0d9ef13 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "orangehill/iseed", + "name": "phill54/iseed", "description": "Generate a new Laravel database seed file based on data from the existing database table.", "keywords": ["laravel", "generators", "seed", "artisan"], "license": "BSD-2-Clause",