Ad

Creating Form With Relationship OctoberCMS

Let's say that I have a simple one to one relationship between to Models, User and Address. I'm attempting to extend the User (using the front-end User Rainlab plugin) to have such a relationship. I've suceeded in attaching the Address to the UserModel using a Behavior:

$model->hasOne['address'] = ['First\Second\Models\Address'];

The problem I have is when putting together a new tab for adding an address to a user on the backend. I have a new tab designated with all of the relevant fields:

street:
    label: "Address"
    tab: "Profile"

address_2:
    label: "Address 2"
    tab: "Profile"

company_name:
    label: "Company"
    tab: "Profile"
    span: left

zip:
    label: "Zip"
    tab: "Profile"
    span: right

city:
    label: "City"
    tab: "Profile"
    span: left

state:
    label: "State"
    placeholder: "Select"
    type: dropdown
    tab: "Profile"
    span: right

I run into a few issues here, however. First, for each of these, I want to reference an address model as opposed to the user model (as in, I want the getStateOptions to pull from the Address model as opposed to User). Second, on save, it will (logically) try to insert into User rather than Address.

I suppose I could use setters for each property, but that is a bit messy as it ties the state of one Model directly to another. Is there anyway in the YAML specification to tell the form to attach these values to the related Model?

Ad

Answer

In your fields.yaml file, you use the relationship name to do what are you describing.

address[street]:
    label: "Address"
    tab: "Profile"

address[address_2]:
    label: "Address 2"
    tab: "Profile"

...

That's it!

Ad
source: stackoverflow.com
Ad