phpnoob
phpnoob

Reputation: 5

Cakephp:: Get data from others table?

I have this model called User, this model use "Users" table. This model is specially designed for admin to login. This model has "id, username, password, created, modified" fields.

Now, If I want to add more users, "normal users", users that can't login. What should I do?

Currently, I created new model called "Person", use "people" table, which has "id, username, email" fields.

From my admin panel(User model), how do I view the normal users(person model) data?

Upvotes: 0

Views: 789

Answers (2)

davidwu
davidwu

Reputation: 619

The classic way to handle this is to store all people in the users table and then add a group_id foreign key to link to the Groups table. Your groups table then has 1, Admin and 2, Normal User as the groups.

So your users table would look like this

users
-----
id, username, password, group_id, created, modified

and groups

groups
------
id, name

Link up your Users model to your Groups model with a belongsTo relationship

var $belongsTo = array('Group');

Now in your admin page you can see each type of user and tell if they are admin users or normal users with a simple fetch, or you can filter by group which group you want to look at.

This also plays really nicely if you are utilizing ACL component to handle access to your app. http://book.cakephp.org/view/1242/Access-Control-Lists

Upvotes: 0

Peng Qi
Peng Qi

Reputation: 1462

first, you are talking about authentication and authorization, you can check out the built-in acl and auth in cakephp.

second, to use Person model

$personModel = ClassRegistry::init('Person');
$person = $personModel->findById($id);

Upvotes: 0

Related Questions