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

ukebako8008
ukebako8008

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.

Last updated

ukebako8008
ukebako8008

I have deleted the solution posted earlier, because with this solution the new related model is not created...

Last updated

ukebako8008
ukebako8008

The only way to fix this problem that I found, is rather dirty solution.

Let's say, my parent model class is Item. Then I can access it's id directly from the post request:

$item = Item::find(post('Item.id'));

This code works only if you have id field defined in fields.yaml of parent model.

If not, you can create custom partial field and generate hidden input and "catch" it from post() when related model's form is rendering to be displayed in popup.

Last updated

Tschallacka
Tschallacka

Very old topic, but strangely relevant for me now.

I added the following to the parent form controller in update.htm

<?= Form::open(['class' => 'layout','data-request-data'=>'quote_id:'.$formModel->getKey()]) ?>

and then in the child model where I needed it

 $id = $this->exists ? $this->quote_id : post('quote_id');
hpanic32253
hpanic32253

2 years later I face the same issue! Has this issue been addressed in later versions of october?

1-5 of 5

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