Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Cakephp query 2 tables

Posted on 2010-08-17
Medium Priority
Last Modified: 2013-11-10
Hi all,
I have an issue with Cakephp...

! have a Model it "User", then a controller and a view.

Model1 currently lists records in table "users" but I have another table that has a user_id field and I need to be able to display records from both tables as they are related and should be linking through "" and "othertable.user_id" fields.

In the View, I need to list records using both tables.

How do I do this please?

I need a simple short example so I can understand it as I've been all over the net and none of those samples work for me.

Hope someone can help


Question by:error77
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Accepted Solution

ropenner earned 2000 total points
ID: 33459995
create view my_new_view as select * from users u, othertable o where =o.user_id

this SQL statement will create your view ... however in this example, if user_id cannot be matched to in the USERS table, it will not showup as a row in the view.

Expert Comment

ID: 33460005
there are many other ways to do this with JOINS (right, left and inner joins) but I think this is the simplest way for you to start.

Assisted Solution

ropenner earned 2000 total points
ID: 33460019
If you need to know the syntax in PHP.

# assuming you've connected to a database somewhere before this point.

mysql_query("create view my_new_view as select * from users u, othertable o where =o.user_id");
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 33460084
Hmm ... Isn't it done differently with CakePhp?

This is how I've done it:

The Model:

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

The Controller:

class UsersController extends AppController {
var $name = 'Users';
function index() {
$this->set('users', $this->User->find('all'));

And the View:

foreach ($users as $user):

echo $user['User']['id'];
echo $html->link($user['User']['title'],
array('controller' => 'users', 'action' => 'view', $user['User']['id']));

So if I wanted to display extra fields from another table where theOtherTable.user_id is the same as ...

How would I do that in Cake please?




Author Comment

ID: 33461952
Anyone please? It's urgent :o/

Author Comment

ID: 33497218
Hi ropenner,

I am not using the baking tool. Doing it manually.

I'm almost there .. Just need to be able to submit (update) a form into fields from different tables.

Can you please help?


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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.

670 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