Ad

Is There Any Reorder Event That I Can Listen To?

- 1 answer

In the backend, I need to export data file whenever my model is modified, no matter it's saved, updated or reordered. The following function works just fine except the reorder (drag & drop feature, aka the Reorder behavior). Can anyone suggest a way to deal with the reorder?

// after the model is saved, either created or updated.   
public function afterSave()
{
  // export data here or fire the my export event
}
Ad

Answer

hmm it seems there is trait which is doing soting work sorting and saving

and its located here vendor\october\rain\src\Database\Traits\Sortable.php its added to model to make it sort-able.

/**
 * Sets the sort order of records to the specified orders. If the orders is
 * undefined, the record identifier is used.
 * @param  mixed $itemIds
 * @param  array $itemOrders
 * @return void
 */
public function setSortableOrder($itemIds, $itemOrders = null)
{
    if (!is_array($itemIds)) {
        $itemIds = [$itemIds];
    }

    if ($itemOrders === null) {
        $itemOrders = $itemIds;
    }

    if (count($itemIds) != count($itemOrders)) {
        throw new Exception('Invalid setSortableOrder call - count of itemIds do not match count of itemOrders');
    }

    foreach ($itemIds as $index => $id) {
        $order = $itemOrders[$index];
        $this->newQuery()->where($this->getKeyName(), $id)->update([$this->getSortOrderColumn() => $order]);
        // ======================================= 
        // YOUR EXPORT CODE CAN BE HERE
        // =======================================
    }
}

this method is called when sorting happens , so may be just copy this method code in your model and override default behavior write your export logic inside this method and it will be called every time sorting happens

so far i guess this is the simplest logic i found.

if any doubt please comment.

Ad
source: stackoverflow.com
Ad