CakePHP: How to build a User's Profile page

3.4k views Asked by At

How do we build a profile page that outputs the user's data? and this page can only be viewed by the user who logged in. It's something like when we go to our profile page and view our own username, password, email, address..etc. Then we may edit it by ourselves. It, of course can't be edited by other users.

I'm confused with the need of a profile table, now I think we would not need it? we can just populate the data using some PHP logic on a page we create as profile.ctp ?

This is confusing, I followed this http://book.cakephp.org/#!/view/1041/hasOne and created a profile table with some fields my users table has, and then with a foreign key called user_id. I checked on User and Profile model both are correctly defined in the relationship. I have this in Profile model:

var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )

and this in User model:

var $hasOne = 'Profile'; 

As I browse to my profile/index there are field names without any records. It's empty set. I thought it was supposed to retrieve data from the users' table ??

What's the best way to create a profile page for the existing users.. and the upcoming registrations ?

1

There are 1 answers

5
Oleksandr Kruk On BEST ANSWER

You do not need a profile table (if you already have a users table with their info).

One way of having this done is, after user validation, compare his id with id of the user which profile he wants to visualize. If those match, then it's a user who is viewing his own profile, and you can let him view his info.

Ofcourse there's a great deal of security issues you would have to take into account if you are thinking to make this a public accessible web site.