Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Create Table When Using 'oil generate model' Confusion
  • Hello,

    I am totally new to FuelPHP, ORM and migrations in general so sorry if I come accross like a noob but I've been struggling with this for a few hours now so I thought I'd ask for help. I think I'm either doing something wrong or missing something fundamental.

    I am trying to create a users model, for simplicity let's say it just has a string representing name.

    I was under the impression that using the following two Oil commands would create a users model, and an associated migration which after running would build an associated table:

    php oil generate model user name:string
    oil refine migrate

    This does successfully create the model and migration, but running the second command doesn't build the table in the database.

    If I run these commands on the other hand:

    php oil generate migration create_user name:text
    oil refine migrate

    The migration is created and the table is built in my database. I noticed that prepending 'create_' to the migration name made it possible to create the table, whereas leaving it off (i.e php oil generate migration user name:text) doesn't insert the table to the DB. I noticed the generated migrations with and without the 'create_' are significantly different.

    So my question ultimately is, how do I create the model, associated migration which creates the table? Or, am I totally misunderstanding something? 

    Thanks!



  • If I do that here (using 1.5/develop at the moment), it generates:
    <?php

    namespace Fuel\Migrations;

    class Create_users
    {
            public function up()
            {
                    \DBUtil::create_table('users', array(
                            'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
                            'name' => array('constraint' => 255, 'type' => 'varchar'),
                            'created_at' => array('constraint' => 11, 'type' => 'int'),
                            'updated_at' => array('constraint' => 11, 'type' => 'int'),

                    ), array('id'));
            }

            public function down()
            {
                    \DBUtil::drop_table('users');
            }
    }
    which looks fine to me?
  • Checked 1.4 as well, which generates exactly the same code.
  • Hi Harro, thanks for helping me out again. It's still acting a little unexpectedly for me though. I've provided a sample command and it's output below in case you (or anyone else) can shine any light on my problem.

    Command:

    oil generate model somerandomthing sometext:string

    Generates model 'app/models/somerandomthing.php':

    <?php

    class Model_Somerandomthing extends \Orm\Model
    {
    protected static $_properties = array(
    'id',
    'sometext'
    );

    protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
    ),
    'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_save'),
    'mysql_timestamp' => false,
    ),
    );
    }

    and migration 'app/migrations/001_create_somerandomthings.php':

    <?php

    namespace Fuel\Migrations;

    class Create_somerandomthings
    {
    public function up()
    {
    \DBUtil::create_table('somerandomthings', array(
    'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
    'sometext' => array('constraint' => 255, 'type' => 'varchar'),
    'created_at' => array('constraint' => 11, 'type' => 'int'),
    'updated_at' => array('constraint' => 11, 'type' => 'int'),

    ), array('id'));
    }

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

    It all looks good but doesn't create the table when 'oil refine migrate' is run?

    I do get this in the CLI if this helps:
    'Already on the latest migration for app:default.'

    Thanks.
  • On another note, how did you manage to format the code in your answer?
  • HarroHarro
    Accepted Answer
    I haven't had time to figure out how to add a 'code' button to this editor, and the forum maker isn't really helpful... I click on the "Show source" button, then enclose the code in <pre> ... </pre>.

    What version of Fuel are you on?

    Your model is missing the 'created_at' and 'updated_at' fields in the properties list, to this is not going to work. That bug is fixed some time ago (but might be in 1.5/develop).

    When I run your migration:
    [hverton@catwoman] $ php oil r migrate
    Performed migrations for app:default:
    001_create_somerandomthings
    So the code works fine.

    If you get 'Already on the latest migration', your migration tracking data is out of sync. Migrations are tracked both in the database (a table called migration) and a config file in your environment folder called migrations.php.

    If there is already an entry in one of them, oil will not run it again.

    So you can't just delete the table through the backdoor and then run the migration again. You'll have to run a 'migrate:down' to revert the last migration, or if you delete all, also delete the migration table and config file.
  • Thanks so much (again!) - deleting the migrations file solved my issue.
  • Cool. Happy codin' !

Howdy, Stranger!

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

In this Discussion