Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I switch to \Orm\Model_Soft
  • I have an existing Model_Blog that extends \Orm\Model with one has_many and belongs_to relationship. Can I use oil to modify the database in order to switch to \Orm\Model_Soft?
  • If you mean generate, no. If you mean a migration for a schema change, yes.
  • I tied this command but that got me nowhere, can you please point me in the right direction?

     oil refine migrate --soft-delete

  • So according to the documentation I added a deleted_at field to my migrations file, so now to run the update can I do something like oil refine migrate post:down and oil refine migrate post:up ?


    I also updated the observers in my model

        protected static $_observers = array(
            'Orm\Observer_CreatedAt' => array(
                'events' => array('before_insert'),
                'mysql_timestamp' => true,
                'property' => 'my_created',
            ),
            'Orm\Observer_UpdatedAt' => array(
                'events' => array('before_update'),
                'mysql_timestamp' => true,
            ),
        );


    namespace Fuel\Migrations;

    class Create_post
    {
     public function up()
     {
      \DBUtil::create_table('blog_post', array(
       'id'          => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
       'name'        => array('constraint' => 255, 'type' => 'varchar'),
       'slug'        => array('constraint' => 255, 'type' => 'varchar'),
       'content'     => array('type' => 'text'),
       'category_id' => array('constraint' => 11, 'type' => 'int'),
       'user_id'     => array('constraint' => 11, 'type' => 'int'),
       'created_at'  => array('constraint' => 11, 'type' => 'int', 'null' => true),
       'updated_at'  => array('constraint' => 11, 'type' => 'int', 'null' => true),
       'deleted_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),

      ), array('id'));

  • OK so some of my previous attempts at oil refine migrate --version=00X deleted my data. Luckily, as one always should I made a backup copy of my database before tinkering with it. I restored it and everything was well. Next according to the documentation i ran 

    oil generate migration add_deleted_at_to_TABLE_NAME deleted_at:int
    oil refine migrate

    Remember you still have to add the observers in your model for the soft delete. Hope this helps someone.

    UPDATE: There is something still not working but at least I was able to update the DB. Now my app cannot find any of the posts in the DB. I will keep troubleshooting.
     
  • SOLVED ! When you convert a regular \Orm\Model into an \Orm\Model_Soft you must ensure to add a 'deleted_at' INTGER NUL . IT MUST BE NUL or else it will be scene as deleted.
  • In MySQL you can use the following command to change the column to accept NULL values. 

    ALTER TABLE [TABLE NAME] modify [COLUMN NAME] integer null;

    Or alternatively, in the migration file you can add 'deleted_at' => array('constraint' => 11, 'type' => 'int', 'null' => true), in the public function up() if you will be using oil to generate the tables

Howdy, Stranger!

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

In this Discussion