Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to extend oil in the right way?
  • Hello,

    when I make a new model with oil, the database table always has fields like id, created_at and updates_at automatically.

    What is the correct or official way if I want to extend the automated generated fields always?
    I would like to have created_by and modified_by in every motel automatically.

    Thanks for your help.

    Reagrds,
    Kay
  • Due to the way oil works, the classes are not easy to extend (they are not aliased like the core classes).

    A possible workaround is to copy the classes you want to change to another location (for example app/classes/oil), and then in the oil bootstap file, after the line

    Package::load('oil');
    do

    Autoloader::add_classes(array(
        'Oil\\Generate_Migration_Actions'    => APPPATH.'/classes/oil/generate/migration/actions.php',
    ));
    for the classes you want to replace.

    If it is easier, the views that are used to generate the code can be copied to app/views without problems. If they exist, oil will use them instead of the default ones in the package. It might be easier to go this route, if you don't have to change too much logic.
  • Thanks, that sound good.

    Can you let me also know, where the fields created_at and modified_at are extended in oil?

    I really cannot find the area where this is done, so that I can extend it.

    Thanks, Kay
  • In oi/classes/generate.php, starting line 263 (on 1.5/develop), which is this line:

    if (\Cli::option('crud'))
    {
  • Thanks Harro,
    but this is for the Model, what I look for is how to extend this for the migration filte to built.

    I have extended it also for the Model like:


    if (\Cli::option('crud'))
            {
                if($created_at = \Cli::option('created-at'))
                {
                    is_string($created_at) or $created_at = 'created_at';

                    $contents .= <<<CONTENTS

        protected static \$_created_at = '$created_at';

    CONTENTS;
                }

                if($created_by = \Cli::option('created-by'))
                {
                    is_string($created_by) or $created_by = 'created_by';

                    $contents .= <<<CONTENTS

        protected static \$_created_by = '$created_by';

    CONTENTS;
                }           

                if($updated_at = \Cli::option('updated-at'))
                {
                    is_string($updated_at) or $updated_at = 'updated_at';

                    $contents .= <<<CONTENTS

        protected static \$_updated_at = '$updated_at';

    CONTENTS;
                }


    But when I let run oil and make the Model, this field is not appearing.

    Any idea why?

    Thanks
    Kay
  • How did you extend it?

    The migration is generated a bit further down, by calling static::migration().

    The code above that, where it adds the created_at etc, also adds the fields to $args, which later is passed to migration() to generate the migration. So add your field to that, same way the others are added.

Howdy, Stranger!

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

In this Discussion