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?
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" ...
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page