Reputation: 24077
I have managed to join 2 of my tables clients
and risk_codes
to each other where clients.risk_code_id
is a foreign key for risk_codes.id
.
In my edit view for Clients I ouput a form using the HTML Helper. E.g. to add an input to edit clients.name
I would use echo $this->Form->input('name');
Given that risk_codes
is a separate table/model how would I output a select dropdown with the options being risk_codes.name
and the values being risk_codes.id
?
The tables are linked like so:
Client belongsTo RiskCode
RiskCode hasMany Client
Upvotes: 0
Views: 975
Reputation: 60463
In your RiskCode
model make sure that displayField
is either set to null
or name
(the latter is one of the defaults):
public $displayField = 'name'; // or null;
In the controller set a list of risk codes for the view:
$this->set('riskCodes', $this->Client->RiskCode->find('list'));
And in the view simply reference the appropriate foreign key field name:
echo $this->Form->input('risk_code_id');
CakePHP will automatically create an appropriate select list, using the models id
and displayField
field values from the list set as riskCodes
.
ps. Many questions like this are answered in the Cookbook, and can also be figured out by using CakePHP to bake the controllers and views.
Upvotes: 1