Inspired from Laravel Artisan and orangehill/iseed for Laravel.
- Modify your controller that host all the function from this library to something like this:
<?php defined('BASEPATH') or exit('No direct script access allowed');
use Virdiggg\SeederCi3\MY_AppController;
class App extends MY_AppController
{
/**
* Hooks for migrate() function.
* If you want to run a callback after migrating a table,
* ex.: create a log file after migration or run grant privileges query for a role.
*
* @return bool $this
*/
private $migrateCalled = false;
public function __construct()
{
parent::__construct();
}
public function migrate()
{
parent::migrate();
$this->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');
}
}
}- Install this library with composer
composer require virdiggg/seeder-ci3 --dev- Create a controller to host all the function from this library. Example is
application/controller/App.php
<?php defined('BASEPATH') or exit('No direct script access allowed');
use Virdiggg\SeederCi3\MY_AppController;
class App extends MY_AppController
{
/**
* Hooks for migrate() function.
* If you want to run a callback after migrating a table,
* ex.: create a log file after migration or run grant privileges query for a role.
*
* @return bool $this
*/
private $migrateCalled = false;
public function __construct()
{
parent::__construct();
}
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');
}
}
}- Create a config file named
seeder.phpor runphp index.php <your controller name> publish
php index.php app publishFile should contains
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* Type of migration, sequential or timestamp. Default to 'timestamp'.
*
* Optional, we will take the value from migration.php if not present
*/
$config['migration_type'] = 'timestamp';
/**
* Path of migration file. Default to 'ROOT/application/migrations'.
*
* Optional, we will take the value from migration.php if not present
*/
$config['migration_path'] = APPPATH . 'migrations' . DIRECTORY_SEPARATOR;
/**
* List of additional table rows with datetime data type.
*
* Default to "['created_at', 'updated_at', 'approved_at', 'deleted_at']".
*/
$config['date_time'] = [];
/**
* Name of database connection. Default to 'default'.
*/
$config['db_conn'] = 'default';
/**
* List of additional function to be called in constructor. Default to [].
*/
$config['constructors'] = [
'controller' => [
'$this->authenticated->isAuthenticated();',
],
'model' => [
'$this->load->helper("string");',
],
'seed' => [
'$this->load->helper("string");',
],
'migration' => [
'$this->load->helper("string");',
],
];cd c:/xampp/htdocs/codeigniter && php index.php app helpcd c:/xampp/htdocs/codeigniter && php index.php app publishcd c:/xampp/htdocs/codeigniter && php index.php app tidycd c:/xampp/htdocs/codeigniter && php index.php app migrate- Add
--to=1to run migration number . Optional. Default is the latest number in your database min 1.
cd c:/xampp/htdocs/codeigniter && php index.php app rollback --to=1- Add
--limit=5to limit the query result. Optional.
cd c:/xampp/htdocs/codeigniter && php index.php app seed users --limit=10How to create Migration file: php index.php <your controller name> <your function name> migration [--args].
- Add
--soft-deleteto add soft delete parameter. Optional.
cd c:/xampp/htdocs/codeigniter && php index.php app migration users --soft-delete- Add
--rto generate resources. Optional.
cd c:/xampp/htdocs/codeigniter && php index.php app controller Admin/Dashboard/Table --r- Add
--rto generate resources. Optional. When using [--r] and your DB is PostgreSQL, you will have a function to create or update a row (storeOrUpdate), please read the comment before you decide to use them. Example:
// 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
--cto generate its controller file as well. Optional. - Add
--mto generate its migration file as well. Optional. - Add
--soft-deleteif your model using soft delete. Optional. When used along with--m, migration will have soft delete fields too.
cd c:/xampp/htdocs/codeigniter && php index.php app model Admin/Users --r --c --m --soft-delete