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 6039271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:20:39+00:00 2026-05-23T06:20:39+00:00

I have two models in cakePHP, one is Parts and the other is Orders

  • 0

I have two models in cakePHP, one is Parts and the other is Orders.

Parts is a simple model with an id column plusaction multiple fields such as customer, part number, description etc. Each part has its own unique id.

Orders is another simple model with a field called *part_id* which represents an id from the Parts model.

In an action, in the Orders controller I wll need to pull all the orders for that particualr view, find information about that order from the Parts database and put it together in an array ready to be passed to the view.

For example that tables might look like:

ORDERS

Part_id   id
2         5
1         7

PARTS

customer  partnumber  description    id
Google    XHA-V1      widget_1       1
Yahoo     YVS-XN      widget_5       2

Since I will have 50-100 orders in any one view, I need an efficient way to reference each order’s *part_id* in the Parts database and combine this into one array. Its the searching of the Parts database multiple times that I’m unsure how to achieve effectively.

Note: I guess the solution could be cake or pure PHP.

  • 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-23T06:20:39+00:00Added an answer on May 23, 2026 at 6:20 am

    If you have correctly defined the relation in your Order Model, you should be able to retreive all the corresponding Parts

    $this->Orders->find('all');
    

    cakephp will do it in two queries:
    – a SELECT for all the Orders
    – a SELECT for all the Products where product.id in ( list_of_Orders_ids )

    and then it will contruct an array in a proper format

    If you need an special query, you could also “manually” set the joins for the query, but you’ll need to set the recursive to -1:

    $this->Orders->find('all',array('recursive'=>-1,
                                    'fields'=>array('Order.*','Part.*'),
                                    'joins'=>array(array('table' => 'parts',
                                                         'alias' => 'Part',
                                                         'type' => 'INNER', //or left
                                                         'conditions' => array('Part.id = Order.part_id')))))
    

    Good Luck!

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

Sidebar

Related Questions

I have two models, one is called labor_report and the other is employee, My
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models, A and B, and one light, L. I would like
I am learning the Model relationship types in cakephp. I have built two tables
I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):
I have two models in my CakePHP application: NewsArticle and Image . A news
I have two models, one called Notes and one called Comments. Comments can be
I have two models in my CakePHP application: NewsArticle and Image . Image is
I have a simple friends system built in CakePHP with two tables: Users and

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.