This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Just use two entries in the hasOne array:
public $hasOne = [
'physicalAddr' => 'Acme\Blog\Models\Address',
'mailingAddr' => 'Acme\Blog\Models\Address'
];
I think you're over doing it, a single belongsTo is all that's needed for the reverse, I think.
I think you can also use the following definition instead of manually "patching" this with formBeforeCreate()
:
public $belongsTo = [
'address_physical' => ['Acme\Plugin\Models\Address', 'default' => true],
'address_mailing' => ['Acme\Plugin\Models\Address', 'default' => true],
];
ref. https://octobercms.com/docs/database/relations#one-to-one (scroll-down to the Default models
section)
Alternatively, you can also use belongsToMany relationshif with a pivot "type" field:
In Business Class:
public $belongsToMany = [
'addresses' => [Address::class, 'pivot'=>['type']],
];
And in Address Class:
public $belongsToMany = [
"businesses" => [Business::class, 'pivot'=>['type']],
];
You would have a joining table that would have a "business_id", "address_id" and "type" field. An advantage is that an address could also be used for more than one Business and for both the physical and mailing address of a business. Also, if you decide to add more address types to your Business, there is no need to modify the database tables.
ref. https://octobercms.com/docs/database/relations#many-to-many
If you go with the belongsToMany with pivot data, add this to your businesses/config_relation.yaml file (Businesses controller):
addresses:
...
pivot:
form:
fields:
pivot[type]:
label: Address Type
type: dropdown
options:
physical: Physical Address
mailing: Mailing Address
Last updated
1-10 of 10