quick best-practices question(s).
I have as association as follows:
User hasOne UserBasicInfo
UserBasicInfo has a select element for country (in which the user is living).
The select element is populated by a simple ‘countries’ table (id, country name, abbreviation)
Is it best to use Model::query() for getting the data from the countries table? Or should I create a model for this data?
If creating a model, should I use an association with the UserBasicInfo model, Or should I just use the $uses variable?
If it sways the answer, there are actually many select fields populated in a similar fashion.
It might. Do you have multiple Country select boxes, or are these select fields all pulling from different tables? If the former, you can use the
optionskey inFormHelper::select()to populate multiple selects from one array:Controller:
View:
Otherwise, I typically handle this situation by creating the proper associations between my models, and then using either
Model::find('list')or a custom Model method to retrieve the options. If you name your variables properly, the options variable will automatically be used as the options for your select:Controller:
View: