2
votes

I'm using OctoberCMS, Apache, and PHP7.

I created a custom Plugin using the Builder.

I'm able to add records, everything is working good. Except if I press the Reorder records button it display a list of empty records.


Is there a guide on how to set this up?

I'm looking at:
https://octobercms.com/docs/database/traits#nested-tree https://octobercms.com/docs/api/october/rain/database/traits/nestedtree

I added to the Model:

use \October\Rain\Database\Traits\NestedTree;

And to the database columns:

parent_id, nest_left, nest_right, nest_depth.


Where do I put $table->integer('parent_id')->nullable();?

If I put it in the Model it gives error Parse error: syntax error, unexpected '$table' (T_VARIABLE), expecting function (T_FUNCTION).


Records

Reorder Records Button


Reorder Records: Empty List

Empty Records

1

1 Answers

2
votes

First of all, you do not need to use nestable as we are not dealing with trees and there seems a guide but it is brief.

We can be just happy with \October\Rain\Database\Traits\Sortable for sorting only as we do not need tree we can skip adding these

use \October\Rain\Database\Traits\NestedTree;
parent_id, nest_left, nest_right, nest_depth

you need a specific column name sort_order if we use trait however we can change this if we need by defining const SORT_ORDER = 'my_sort_order'; in our model.

As you have already build tables you can update your table definition using builder plugin and add sort_order field to your table.

or manually you can use this script and add it to version.yaml file [ plugins\hardiksatasiya\demotest\updates ( respectively in your plugin ) ]

version.yaml

1.0.19:
    - 'Updated table hardiksatasiya_demotest_sorting'
    - builder_table_update_hardiksatasiya_demotest_sorting_3.php

builder_table_update_hardiksatasiya_demotest_sorting.php

<?php namespace HardikSatasiya\DemoTest\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class BuilderTableUpdateHardiksatasiyaDemotestSorting extends Migration
{
    public function up()
    {
        Schema::table('hardiksatasiya_demotest_sorting', function($table)
        {
            $table->integer('sort_order')->default(0)->change();
        });
    }

    public function down()
    {
        Schema::table('hardiksatasiya_demotest_sorting', function($table)
        {
            $table->integer('sort_order')->default(null)->change();
        });
    }
}

Now, add Trait to your Model

<?php namespace HardikSatasiya\DemoTest\Models;

use Model;

/**
 * Model
 */
class Sort extends Model
{
    use \October\Rain\Database\Traits\Validation;
    use \October\Rain\Database\Traits\Sortable;

    ....

Now either you can add manually all the needed files or use builder tool for it.

I prefer using Builder to add a controller and needed file

Make sure you tick Reorder behavior

enter image description here

To show name in reorder list, we need to set this property from which field we need to derive a name to show in sorting list.

enter image description here

It will look like this with name as a sort attribute name

enter image description here

Side Menu [ plugin.yaml and controllers]

enter image description here

enter image description here

If you have further doubts please comment.