Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is there a way to insert data in a migration?
  • Let me explain, im creating a wizard to configure all the things i need of a fullphp project, so i ask for the company name, email, phone and so on, and i need to insert all of that in the "Installation", im using plane php for that script
  • You don't want to know what we all do in a migration. Everything but make coffee! :-)

    Altough often suggested, migrations are not solely for database schema changes. Everything needed to migrate an application from version A to version B can be put in a migration file, like file and directory creation, data manipulations, loading data, etc.
  • Can you please explain to me the way i can use to populate the data of a table in a migrate generation?
  • HarroHarro
    Accepted Answer
    It depends on your situation? How much data? Where is it coming from?

    It could be as simple as:

    <?php
    namespace Fuel\Migrations;

    class Finance_Currencies
    {

        function up()
        {
            // table currency codes
            \DBUtil::create_table('finance_currencies', array(
                'id' => array('type' => 'int', 'constraint' => 11, 'auto_increment' => true),
                'code' => array('type' => 'char', 'constraint' => 3),
                'name' => array('type' => 'varchar', 'constraint' => 100, 'default' => ''),
                'symbol' => array('type' => 'varchar', 'constraint' => 15, 'default' => ''),
                'country' => array('type' => 'char', 'constraint' => 2, 'default' => ''),
                'decimals' => array('type' => 'tinyint', 'constraint' => 1, 'default' => 2),
                'user_id' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
                'created' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
                'updated' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
            ), array('id'));

            \DB::insert('finance_currencies')->set(array('id' => '1','code' => 'AED','name' => 'UAE Dirham','symbol' => '','country' => 'AE', 'user_id' => 1, 'created' => time()))->execute();
            \DB::insert('finance_currencies')->set(array('id' => '2','code' => 'AFN','name' => 'Afghani','symbol' => '؋','country' => 'AF', 'user_id' => 1, 'created' => time()))->execute();
            \DB::insert('finance_currencies')->set(array('id' => '3','code' => 'ALL','name' => 'Lek','symbol' => 'Lek','country' => 'AL', 'user_id' => 1, 'created' => time()))->execute();
            \DB::insert('finance_currencies')->set(array('id' => '4','code' => 'AMD','name' => 'Armenian Dram','symbol' => '','country' => 'AM', 'user_id' => 1, 'created' => time()))->execute();
            \DB::insert('finance_currencies')->set(array('id' => '5','code' => 'ANG','name' => 'Netherlands Antillean Guilder','symbol' => 'ƒ','country' => '', 'user_id' => 1, 'created' => time()))->execute();
            \DB::insert('finance_currencies')->set(array('id' => '6','code' => 'AOA','name' => 'Kwanza','symbol' => '','country' => 'AO', 'user_id' => 1, 'created' => time()))->execute();

            // and so on...
        }

        function down()
        {
            \DBUtil::drop_table('finance_currencies');
        }

    }

  • Thanks a lot, it will be helpful but i did the inserts with the ORM 
  • Also possible, I tend not to do that if it's not needed, for performance reasons.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion