cakephp association confusion

Posted on 2014-08-03
Last Modified: 2014-08-07

I have a simple question on associations in cakephp.
I have a user , role and tutor table.
The  issue is how do i define this where I have a admin user who can view tutors but there is no tutor entry with this user? Or do I need to bother  as not all users are tutors anyway.

I can create a user that is a tutor and have the userID in the tutor table as a foreign key.
The role is the user type like admin, tutor etc which is linked to the user.

 public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'

 public $hasOne = array(
        'Tutor' => array(
            'className' => 'Tutor',
            'dependent' => true
Question by:jagguy
    1 Comment
    LVL 14

    Accepted Solution

    It's been a while since I've used CakePHP but wouldn't you want to handle the admin case in your controller or business object?  If the user is an admin then they could access a method that pulls all tutors or all admins or all users.  If the user is not an admin then they could see only the tutors or whatever subset of the data you want them to see based on your business rules.

    If the functions of an admin user and a non-admin user are really different then you may want to consider having two different models defined and then load the appropriate model in your controller based on the role of the user.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now