This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

neilcarpenter
neilcarpenter

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('delegates') ?> 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.

Eoler
Eoler

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

Heh, I'm working on refreshing similar price sum calculations at the moment - exploring documentation here: https://octobercms.com/docs/backend/relations#extend-refresh-results

neilcarpenter
neilcarpenter

Hi Eoler,

How can I access the model details that I'm working on (so in my case, the Booking Model) to return the updated info to the view inside that method though?

neilcarpenter
neilcarpenter

The relationExtendRefreshResults only runs when I delete a relation, not when one is created... so thats not really any good to me Eoler. Thanks though!

Eoler
Eoler

neilcarpenter8619441 said: The relationExtendRefreshResults only runs when I delete a relation, not when one is created... so thats not really any good to me Eoler.

It works for me for add/remove in hasMany/belongsTo relation, you can get model in event handler with $this->formGetModel(), but now how to refresh darn form UI?! Also researching formGetWidget() at the moment...

Eoler
Eoler

Solved! I've added sum calculations to model as accessors, then defined them on backend form (in fields.yaml) and finally made those calc fields dependent on relation field.

neilcarpenter
neilcarpenter

Sorry man, but I don't think that solves my original problem.

Eoler
Eoler

Ok, let's step back and focus on:

neilcarpenter8619441 said: (...) 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?

How are those price attributes calculated? How are they shown on backend form?

Form field dependsOn option works for relation fields, too: https://octobercms.com/docs/backend/forms#field-dependencies

anuruddha34276
anuruddha34276

neilcarpenter8619441 said:

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('delegates') ?> 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.

Did you find a solution for this? I'm facing the same problem!.

Code51
Code51

I had the same problem with a pivot relationship. Update the price of the parent when adding new children.

The onDepends option works when checking a checkbox in the relationship view but not when adding a new child.

I used the following code to refresh the form manually.

$(document).on('ajaxDone', function (event, context) {
        if (context.handler == 'onRelationManagePivotUpdate' || context.handler == 'onRelationManagePivotAdd' || context.handler == 'onRelationManagePivotCreate') {
            $.request('form::onRefresh');
        }
    });

Last updated

st.james.jomuad13119
st.james.jomuad13119

In my case:

Achive it by using 'relationExtendRefreshResults'. Hope this helps.

Controller.php

public function relationExtendRefreshResults() {
    $result['#Form-field-Debt-prev_balance-group'] = $this->formRenderField('prev_balance', ['useContainer'=>false]);

    return $result;
}

Last updated

Utopig Studio
Utopig Studio

st.james.jomuad13119 said:

This last solution is almost working for me, but the value that shows in the related field is not updated. Let's say I do this:

  • Customer has 10 credits
  • I add a subscription to the customer and substract 1 credit. Customer has 9 credits (value in DB is ok). The input shows 10.
  • I add a second subscription to the customer and substract 1 credit. Customer has 8 credits. The input shows 9.

Any clue to what is happening here???

rupert24306
rupert24306

I struggled with this same issue and could not get any of the above solutions to work.

I did it a different way so wanted to share in case it might help others.

I used 'dependsOn' on the field on my form I needed to update and set the field depended on to be the relations field.

I then used the filterFields function in the model of the parent. Something like this:

use Author\Model\Models\Childmodel;

public function filterFields($fields, $context = null)
    {
        $accesslatestchildmodels = Childmodel::where('parent_id','=',$this->id)->orderBy('id','desc')->get();
        $fields->{'parentfield_to_update'}->value = $accesslatestchildmodels[0]->value_to_add_to_form;
    }

Last updated

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.