Working with multiple nested relations in forms

Hi

Just wondering if there was anything built into Yii or applicable packages that can help with the following scenario.

As an example, take these Three tables:


Person

- PersonID

- Name


Phone

- PhoneID

- Number


PersonPhone

- PersonPhoneId

- PhoneId

- PersonId

So a User could have multiple phone numbers. If I’m displaying in a single form:

Person Details

  • Name

Phone

  • Person’s current phone number
  • button to add/update a new phone number to this person. So when the form is submitted it will go through, find if the phone already exists in the system. If it doesn’t exist, it will create a phone number, then create a new record in the PersonPhone for this person and phone number. Similar for update.

I’m yet to find a prebuilt package for this that would help with this. Most ones seems to only be able to go one relation down??

Any help, very welcome. Thanks :slight_smile:

Hi Jonny,

Would you please take a look at the following wiki article?

https://www.yiiframework.com/wiki/836/how-to-createupdate-a-model-with-its-related-items-using-listbox-or-checkboxlist

It handles a similar scenario among Post, Category and PostCategory using the 4th model, i.e., PostWithCategories model.

It doesn’t support the functionality of adding new Category when creating a Post, but I hope it can be easily implemented extending this concept. I mean, you could consider creating PersonWithPhones model that satisfies your needs.

Thanks Softark,

Let me look into this!