Cakephp query 2 tables

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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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");
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

error77Author Commented:
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?



error77Author Commented:
Anyone please? It's urgent :o/
error77Author Commented:
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?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.