Reputation: 3164
I have two tables, users{name,id,age_range_id}
and age_ranges{id,range_name}
.
There are also two models, controllers and the proper view files.
In the views I have the adduser.ctp
file, which hold the proper form.
What I want is to have an input (select) that will hold the options from age_ranges.name
field.
So, in the users model I've added var $hasMany = 'age_ranges';
What's next?
I know that I can use the $this->set
to store the options as an array in the controller and then use it in the view.
but I assume(wrongly?) that by relating the models there is an 'automatic' way to do it.
Which lead me to the question: how?
Upvotes: 0
Views: 1997
Reputation: 6571
I won't repeat what Thorpe and dogmatic have already written. They are both correct.
However, although it's difficult to be sure without seeing your model files, commonsense tells me that in the User model the relationship should be User:hasOne:AgeRange and in the AgeRange model it is AgeRange:hasMany:User, not the other way round as you have written.
Also, you do not specify the table name ('age_ranges') in the relationship, but the Model name ('AgeRange').
See these pages in the manual:
http://book.cakephp.org/view/1001/Understanding-Models & http://book.cakephp.org/view/1039/Associations-Linking-Models-Together
Upvotes: 1
Reputation: 12891
You can do this:
$this->User->AgeRange->find('list')
and pass that to the view for a select statement
Upvotes: 1
Reputation: 7585
add $this->set('ageRanges', $this->User->AgeRange->find('list')); to the controller action
then add $this->Form->input('age_range_id') to the form
Upvotes: 1