winter icon indicating copy to clipboard operation
winter copied to clipboard

Get parent model from its child relation

Open xyz1123581321 opened this issue 3 years ago • 8 comments

xyz1123581321 avatar Oct 06 '21 13:10 xyz1123581321

Is it related to any issue? Which problem or situation are you trying to solve or feature are you trying to implement?

RomainMazB avatar Oct 06 '21 14:10 RomainMazB

We talked about this small improvement on discord with Luke.

xyz1123581321 avatar Oct 06 '21 19:10 xyz1123581321

Ok, just for prosperity I post here the issue from the OC forum you are trying to solve. Just for others who don't know like me what's going on here :)

I have 2 models: Item and ItemPrice Item hasMany ItemPrices ItemPrice belongsTo Item

When I create new ItemPrice from Item update form (using relation controller), I need to access Item model (i.e parent model) when generation dropdown options in related model form. Something like $this->item->... But relation controller doesn't set relation to parent model before related model is added.

RomainMazB avatar Oct 06 '21 19:10 RomainMazB

Did you review it?

xyz1123581321 avatar Oct 11 '21 20:10 xyz1123581321

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days. If this is still being worked on, please respond and we will re-open this pull request. If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

github-actions[bot] avatar Dec 11 '21 00:12 github-actions[bot]

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days. If this is still being worked on, please respond and we will re-open this pull request. If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

github-actions[bot] avatar Feb 11 '22 00:02 github-actions[bot]

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days. If this is still being worked on, please respond and we will re-open this pull request. If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

github-actions[bot] avatar Apr 15 '22 00:04 github-actions[bot]

This pull request will be closed and archived in 3 days, as there has been no activity in this pull request for the last 6 months. If you intend to continue working on this pull request, please respond within 3 days. If this pull request is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Oct 15 '22 00:10 github-actions[bot]

This pull request will be closed and archived in 3 days, as there has been no activity in this pull request for the last 6 months. If you intend to continue working on this pull request, please respond within 3 days. If this pull request is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Aug 22 '23 00:08 github-actions[bot]

@LukeTowers anything else required here?

bennothommo avatar Aug 22 '23 00:08 bennothommo

The way I do this personally is this in the related model constructor:

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    $this->bindEvent('model.form.filterFields', function ($formWidget, $fields, $context) {                                              
        if ($context === 'create') {
            if ($model = $formWidget->model) {
                $model->parent = $formWidget->getController()->relationObject->getParent();
            }
        }
    }
}

mjauvin avatar Aug 22 '23 02:08 mjauvin

I'm concerned about knock-on effects from making this sort of change, is it possible to simplify the problem down to a small demo of the issue that can have test cases documenting the expectations and then verify the solution from there or is it too complicated with how it integrates with the relationcontroller functionality?

Either way I'll need to spend some time on this to refresh my memory on what the original problem was as well as how this solved it and my original concerns. Is there still interest in getting this merged or is @mjauvin's proposed workaround decent enough for anyone else encountering this sort of bump in the road?

LukeTowers avatar Aug 22 '23 07:08 LukeTowers

This pull request will be closed and archived in 3 days, as there has been no activity in this pull request for the last 6 months. If you intend to continue working on this pull request, please respond within 3 days. If this pull request is critical for your business, please reach out to us at [email protected].

github-actions[bot] avatar Feb 21 '24 00:02 github-actions[bot]

I just use the following:

$formwidget->getController()->relationObject->getParent();

mjauvin avatar Feb 21 '24 01:02 mjauvin

Let's close this for now, given there are available workarounds. If someone wishes to take charge of this and get this over the line, feel free to drop a line here and we'll re-open.

bennothommo avatar Feb 21 '24 01:02 bennothommo