Solved

Cakephp query 2 tables

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

! have a Model ..call 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 "users.id" 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

Thanks

0
Comment
Question by:error77
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
ropenner earned 500 total points
Comment Utility
create view my_new_view as select * from users u, othertable o where u.id =o.user_id

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

Expert Comment

by:ropenner
Comment Utility
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.
0
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 500 total points
Comment Utility
If you need to know the syntax in PHP.

<?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 u.id =o.user_id");
?>
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:error77
Comment Utility
Hmm ... Isn't it done differently with CakePhp?

This is how I've done it:

The Model:

<?php
class User extends AppModel {
 var $name = 'User';
 }
?>

The Controller:

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

And the View:

<?php
foreach ($users as $user):

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

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

How would I do that in Cake please?

thanks.

 





0
 

Author Comment

by:error77
Comment Utility
Anyone please? It's urgent :o/
0
 

Author Comment

by:error77
Comment Utility
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?


0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

771 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