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!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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.
…and so on and so on.
…and so on and so on.
Note the name of the tables,
usersandroles, and the field names, specificallyidandrole_id. Those are important.idis normally an auto increment,intfield in your database. There’s other ways to store youridkey 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/(whereinstall_diris the name of the folder you have the framework installed in). Once there create a new file and name ituser.php. Inside this file we’re gonna use some code that looks like this…And, well, there you have it! You’re now free to query the
rolestable from within yourUserclass by calling methods through Cake’s “association chain”,$this->User->Role->find(). You can, and probably should, create aRolemodel just like you createdUser, sans the$belongsTo. Inside it you can define your own methods to interact with therolestable and give the information you need toUser.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.
This should return all user’s usernames (and possibly their
idandrole_idas well) that have an entry in therolestable. 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.