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

adding where option #207

Open
wants to merge 2 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "orangehill/iseed",
"name": "phill54/iseed",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing the name here?

"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",
Expand Down
10 changes: 7 additions & 3 deletions src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}

Expand Down
10 changes: 8 additions & 2 deletions src/Orangehill/Iseed/IseedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +108,8 @@ public function fire()
$dumpAuto,
$indexed,
$orderBy,
$direction
$direction,
$where
),
$table
);
Expand All @@ -128,7 +130,10 @@ public function fire()
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed
$indexed,
$orderBy,
$direction,
$where
),
$table
);
Expand Down Expand Up @@ -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),
);
}

Expand Down