winter
winter copied to clipboard
Get parent model from its child relation
Is it related to any issue? Which problem or situation are you trying to solve or feature are you trying to implement?
We talked about this small improvement on discord with Luke.
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.
Did you review it?
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.
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.
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.
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].
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].
@LukeTowers anything else required here?
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();
}
}
}
}
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?
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].
I just use the following:
$formwidget->getController()->relationObject->getParent();
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.