1

I have 2 models Booking and Attendee with a Many to Many relation

I also have a BookingAttendee pivot model - so I can make use of the afterCreate methods that these have.

In the backend form for a Booking where I want to add attendees - I'm using a partial to render the relation with <?= $this->relationRender('attendees') ?> and everything is working fine.

A booking has some price attributes that I would like to update on the page everytime an attendee is added or removed.

Are there any sort of methods on the relationWidget that happens after a relation is created that would allow me to update a div on the page?

According to the docs, there are these 4 methods that I can use to extend on the RelationController

relationExtendViewWidget()
relationExtendManageWidget()
relationExtendPivotWidget()
relationExtendRefreshResults()

The problem is I don't know how to use these to update content on the page.

  • FYI, you can not use afterSave on a Pivot model. I saw a comment in your github repo where you're wondering why. Pivot model doesn't have hooks to before and after events like a normal model. It's a known issue https://github.com/octobercms/october/issues/2747 – Miloš Stanić Mar 19 '19 at 11:02

1 Answers1

0

You have to add a div with an unique ID to your relation render. For example:

<div id="delegates-relation">
    <?= $this->relationRender('delegates') ?>
</div>

Then in your controller you have to add the relationExtendRefreshResults($field) method and return your new html from the relation render with the id defined above.

/**
 * Update fields after a relation has changed
 *
 * @param $field the relation field that has changed.
 * @return array|null
 */
public function relationExtendRefreshResults($field):? array
{
    if ($field !== 'yourField') { // here you can check which field was updated.
        return null;
    }

    return [
        '#delegates-relation'=> $this->relationRender('delegates'),
    ];
}
Jacob
  • 132
  • 1
  • 9
  • I don't think this fixes my issue unless I'm misunderstanding.. I don't need to render the relation again. On the booking form, I have a #total-price div that I want to update with the new total price once an attendee has been added or removed on the booking. I don't think I have access to the Booking model in the relationExtendRefreshResults() method - and that's only getting fired when an attendee is removed. Not added. :/ – Neil Carpenter Mar 14 '19 at 19:03
  • @NeilCarpenter For me the form gets fired when a new item is created. You can get the booking model if you are in the bookingcontroller in the refresh results method. `/** @var Form $formWidget */` `$formWidget = $this->formGetWidget();` `/** @var $model your model*/` `$model = $this->formGetModel();` – Jacob Mar 14 '19 at 19:51
  • I still don't get relationExtendRefreshResults() fired when an Attendee is added to a booking. The flow is, i click the Add attendee button, a popup is display, I choose the attendee I want to add, then I'm shown a pivot form. Then I click the add button. I'm sure I can't be the only person whos tried to figure this one out – Neil Carpenter Mar 15 '19 at 14:22
  • https://github.com/neilcarpenter007/ncarps-bookings - he's a stripped backed version of what I'm trying to achieve Jacob – Neil Carpenter Mar 18 '19 at 13:28
  • @NeilCarpenter I've tried to fix it, but it looks like a bug in OctoberCMS. I think you should ask the forum or create an issue on github. If you also share your plugin there, maybe someone can help. – Jacob Mar 19 '19 at 18:29