I have two tables:
Group(id/groupname)
People(id/name/groupid)
Group
has many People
In the Group
model:
var $hasMany = array(
'People' => array(
'className' => 'People',
'foreignKey' => 'groupid'
)
);
Is this OK ???
I also want my Group index.ctp to display group and the people that belong to it. Does anybody know how to do this?
tks
First: You need to use the proper naming conventions:
Group(id/name)
Person(id/name/group_id)
If you use the right naming conventions on your table, you can use
cake bake
to auto-generate models, views and controllers.How to do this manually:
In your Group model, you need to set up your relationship:
I'm presuming your GroupsController has the proper
index
function:Next, you need to display this all in your Groups/index.ctp. To do this, you loop through all the groups, and display each one. Each group will have all its people attached to it. If you aren't totally sure what kind of data your controller is passing to the view, you can do a
debug($groups)
in your view to see what the structure is like.This is a VERY bare-bones way you can view all your users with your groups:
You say you want to select a person's group when you create the person:
under PeopleController.php, make sure you have this in your add() function:
And then, in your add.ctp view:
CakePHP's magic should automatically populate the select box with your groups! Hope that helps!