I have a database structure like so:
> Contracts hasMany ContractsTitles
> ContractsTitles hasMany Rates
> Rates belongsTo Formats
For a given Contract, I would like to know how many distinct Formats are assigned to it via these relationships. For example a query like this:
SELECT contracts_titles.contract_id AS contract_id, formats.name AS format FROM rates
LEFT JOIN formats on rates.format_id = formats.id
LEFT JOIN contracts_titles ON rates.contracts_title_id = contracts_titles.id
GROUP BY contracts_titles.contract_id, formats.id;
That would produce a table like:
contract_id | format
--------------------------
1 | Small
1 | Medium
1 | Large
2 | Medium
3 | Small
3 | Large
So what I would like is to be able to quickly access this data as if it were a contain, like if I did ->contain([‘AssignedFormats']) then Contract 1 would have the property assigned_formats with Small, Medium and Large entries.
Is there a way to define this as a relationship, or some other sensible way to make this easily accessible from the Contracts table? Thanks