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

  • SEARCH
  • Home
  • 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 6223849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:33:43+00:00 2026-05-24T08:33:43+00:00

I have a table/model of users, and a certain subset of those users have

  • 0

I have a table/model of users, and a certain subset of those users have an entry in a “roles” table.

What is the standard way to create a model that has access to all the users who have one or more entries/rows in the roles table?

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-24T08:33:44+00:00Added an answer on May 24, 2026 at 8:33 am

    Welcome to Cake!

    One of the coolest and most powerful aspects of Cake is their very capable ORM. Once you figure out Cake’s associations and the way they work with databases it’s hard to go back to something else. But, there’s a couple things we need to make sure we do with our actual physical database schema before we can do this.

    So, let’s work with some “sample” data.

    users
     -------------------------------------------------------
    |  id (PK)  |  role_id (FK)  |  username  |  password   |
     -------------------------------------------------------
    |     1     |         1      |  cspray     |  ad64675   |
     -------------------------------------------------------
    

    …and so on and so on.

    roles
     --------------------------------------------
    |  id (PK)  |              name              |
     --------------------------------------------
    |  1        |  Benevolent Dictator for Life  |
     --------------------------------------------
    

    …and so on and so on.

    Note the name of the tables, users and roles, and the field names, specifically id and role_id. Those are important. id is normally an auto increment, int field in your database. There’s other ways to store your id key but let’s not pile too much on at once.

    Now, all this and we haven’t answered your question yet! Don’t worry we’re about to get to that. But, first double check your database schema and make sure those table names and columns are named according to Cake convention. Don’t worry I’ll wait…

    …

    Ok! Now open up your favorite text editor and navigate to install_dir/app/models/ (where install_dir is the name of the folder you have the framework installed in). Once there create a new file and name it user.php. Inside this file we’re gonna use some code that looks like this…

    class User extends AppModel {
    
        public $belongsTo = array('Role');
    
    }
    

    And, well, there you have it! You’re now free to query the roles table from within your User class by calling methods through Cake’s “association chain”, $this->User->Role->find(). You can, and probably should, create a Role model just like you created User, sans the $belongsTo. Inside it you can define your own methods to interact with the roles table and give the information you need to User.

    I don’t want to go too much into how Cake’s ORM works though, mostly because if you read through the Cake Cookbook and the Cake API you will find a wealth of knowledge ready to be plucked.

    Enjoy your ventures into Cake!


    So, your model associations are all setup and you’re ready to go. But, where do you go? Well, you’ll want to check out Cake’s Model behaviors, particularly one of the most important core behaviors, Containable. This will allow you to do exactly what you want to do.

    Let’s just take a look at some code, I’m going to assume that you’re already familiar with the Model::find() method. If not I’d go check that out real quick so you don’t get too confused. It’s fairly straight-forward though.

    class User extends AppModel {
    
        // provides $this->User->Role
        public $belongsTo = array('Role');
    
        // gives access to various behaviors
        public $actsAs = array('Containable');
    
        // custom function
        public function get_users_with_roles() {
    
            return $this->User->find(
                'contain' => array(
                    'Role' => array(     // tells ORM to query `roles` table
                        'Hour',         // <-- tells ORM to query `hours` table
                        'conditions' => array(
                           'Role.id' => '<> NULL'
                        )
                    )
                ),
                'fields' => array(
                    'User.username'
                )
            );
    
        }
    
    }
    

    This should return all user’s usernames (and possibly their id and role_id as well) that have an entry in the roles table. Note the addition of the new property and the way the “query” is laid out in Cake’s ORM. One thing I could suggest getting comfortable with is multi-dimensional associative arrays, Cake uses a lot of them.

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

Sidebar

Related Questions

In my Yii application, I have a model that represents siteconfig table and have
I have these table rows that have checkboxes: @foreach (var item in Model) {
I have a simple Observer that is watching certain models and writing the model
I have Messages and User models with corresponding tables. The Messages table has such
Suppose I have an Orders table in my database and a corresponding model class
I have the following code to generate a table listing brand/model/submodel/style for a group
I have a Model created with a table called Customers. It contains the following
I have a simple ActiveRecord model called Student with 100 records in the table.
I have a Movies and a Actors table and Casts as join-model. To be
Hi So I have created a quick table: package models; import javax.persistence.*; import play.db.ebean.Model;

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.