https://github.com/virdiggg/seeder-ci3
Migration and Seeder from Existing Database for CodeIgniter 3
https://github.com/virdiggg/seeder-ci3
codeigniter codeigniter-library codeigniter3 migrations php seeder-generator
Last synced: 9 days ago
JSON representation
Migration and Seeder from Existing Database for CodeIgniter 3
- Host: GitHub
- URL: https://github.com/virdiggg/seeder-ci3
- Owner: virdiggg
- License: mit
- Created: 2023-09-06T06:24:59.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-03T02:49:23.000Z (3 months ago)
- Last Synced: 2025-04-22T21:07:48.427Z (9 days ago)
- Topics: codeigniter, codeigniter-library, codeigniter3, migrations, php, seeder-generator
- Language: PHP
- Homepage:
- Size: 95.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A Simple Library Seeder from Existing Database for CodeIgniter 3
![]()
![]()
## Inspired from Laravel Artisan and [orangehill/iseed](https://github.com/orangehill/iseed) for Laravel.
### UPGRADE FROM 1.x to 2.x
- Modify your controller that host all the function from this library to something like this:
```php
migrateCalled = true;
}// If you don't wish to have rollback function
public function rollback() {
return;
}// The rest of your code
public function __destruct()
{
if ($this->migrateCalled) {
// $this->db->query("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO myrole");
// $this->db->query("GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myrole");
// log_message('error', 'PREVILEGES GRANTED');
}
}
}
```### HOW TO USE
- Install this library with composer
```bash
composer require virdiggg/seeder-ci3 --dev
```
- Create a controller to host all the function from this library. Example is `application/controller/App.php`
```php
dbConn = 'default2';
$config->migrationType = 'timestamp';
// Append 'create_date', 'change_date', 'last_access' to the list of $dateTime
$config->dateTime = ['create_date', 'change_date', 'last_access'];
$config->constructors = [
'controller' => [
'$this->authenticated->isAuthenticated();',
],
'model' => [
'$this->load->helper("string");',
],
'seed' => [
'$this->load->helper("string");',
],
'migration' => [
'$this->load->helper("string");',
],
];parent::__construct($config);
}public function migrate()
{
parent::migrate();
$this->migrateCalled = true;
}// If you don't wish to have rollback function
public function rollback() {
return;
}public function __destruct()
{
if ($this->migrateCalled) {
// $this->db->query("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO myrole");
// $this->db->query("GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myrole");
// log_message('error', 'PREVILEGES GRANTED');
}
}
}
```#### Help options: `php index.php help`.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app help
```
#### How to run migration: `php index.php migrate`.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app migrate
```
#### How to run rollback migration: `php index.php rollback [--args]`.
- Add `--to=1` to run migration number . Optional. Default is the latest number in your database min 1.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app rollback --to=1
```
#### How to create Seeder file: `php index.php seed [--args]`.
- Add `--limit=5` to limit the query result. Optional.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app seed users --limit=10
```
#### How to create Migration file: `php index.php migration [--args]`.
- Add `--soft-delete` to add soft delete parameter. Optional.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app migration users --soft-delete
```
#### How to create Controller file: `php index.php controller [--args]`.
- Add `--r` to generate resources. Optional.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app controller Admin/Dashboard/Table --r
```
#### How to create Model file: `php index.php model [--args]`.
- Add `--r` to generate resources. Optional.
When using [--r], you will have a function to create or update a row (storeOrUpdate), please read the comment before you decide to use them. Example:
```php
// In this code, we will insert a new user $param,
// only if there is no user with $conditions in the table
// So, insert into table when there is no row with name = 'myname'
// and username = 'myusername'
$param = [
'name' => 'myname',
'username' => 'myusername',
'password' => password_hash('password1', PASSWORD_BCRYPT),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'created_by' => 'admin',
'updated_by' => 'admin',
];
$conditions = [
'name' => 'myname',
'username' => 'myusername',
];
$res = $this->mymodel->storeOrUpdate($param, $conditions);
// In this code, we will insert a new user $param,
// but since we don't pass the second parameters,
// then we will use the first parameters as $conditions
// but only if they're not in list of $this->exceptions
// So, insert into table when there is no row with name = 'myname'
// and username = 'myusername' and password = hashed string, etc...
$param = [
'name' => 'myname',
'username' => 'myusername',
'password' => password_hash('password1', PASSWORD_BCRYPT),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'created_by' => 'admin',
'updated_by' => 'admin',
];
$res = $this->mymodel->storeOrUpdate($param);
```
- Add `--c` to generate its controller file as well. Optional.
- Add `--m` to generate its migration file as well. Optional.
- Add `--soft-delete` if your model using soft delete. Optional. When used along with `--m`, migration will have soft delete fields too.
```bash
cd c:/xampp/htdocs/codeigniter && php index.php app model Admin/Users --r --c --m --soft-delete
```