Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:01:40+00:00 2026-05-27T19:01:40+00:00

I have two tables: Users and Profiles. A user may have a profile and

  • 0

I have two tables: Users and Profiles. A user may have a profile and a profile must have a user!

The tables are:

Users:
id
username
email
password

Profiles:
firstname
lastname
gender
dob
user_id

So as you can see the profiles table links back to the users via the user_id key

How do I view a profile based on the username of that user?

As it needs to pull information from both tables… so far I have:

public function view ( $username )
{
    $profile = $this->Profile->find('first', array(
                'conditions' => array('User.username' => $username)
            ));

    if (empty($profile))
    {
        $this->cakeError('error404');
    }

    $this->set(compact('profile'));
}

and this is the route for it:

Router::connect('/people/:userName',array('controller'=>'profiles','action'=>'view'),
    array(
        'userName'=>'[A-Za-z0-9\._-]+',
        'pass'=>array('userName')
    )
);

Also here are the Models:

User:

class User extends AppModel
{
    var $name = 'User';
}

I have not specified the user hasOne profile, as the user may not have a profile. e.g admin account

Profile:

class Profile extends AppModel
{
    var $name = 'Profile';

    var $belongsTo = 'User';
}

and here is my view:

<h1><?php echo $profile['Profile']['firstname']; ?> <?php echo $profile['Profile']['lastname']; ?></h1>

But need some steer to get it correct. According to the CakePHP 2.0 book I can do something like: $this->User-Profile-> but I don’t fully understand it or how I could use it here???

For example would this be correct:

$profile = $this->Profile->User->find('first', array(
'conditions' => array('User.username' => $username)
));

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:01:41+00:00Added an answer on May 27, 2026 at 7:01 pm

    According to your routes, you are going to use a ‘User’ model from ProfilesController. For this to work, you should add after the class ProfilesController extends Controller the following line :

    public $uses = array('User','Profile');

    Then you get directly the User model like this :

    $this->User->...

    I may be wrong, but I don’t think you could possibly do $this->Profile->User

    [EDIT :]

    after discussing this, it appears the User Model should really have $hasOne='Profile';

    Because it does not enforce a profile, but just get it from database if it ever exists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using php and mysql I have two tables, a users table and a profiles
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
I have a simple database with two tables. Users and Configurations. A user has
I have two RoR3 application: http://users.domain.local http://profiles.domain.local I created the 'users/models/profile.rb': class Profile <
I have two tables users - id - name - email users_groups - user_id
I have two relational tables: the Profiles table which contains 3 kinds of user
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
If I have two tables - Logins and Users, as follows: Logins LoginIdNo UserIdNo
We have two tables - Tasks and TasksUsers (users assigned to task). Task has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.