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, one is Group, the other Item. A group has many
I have two models generated with generate scaffolding, one is a LogBook the other
I'm using cakephp, I have a model comment with two fields : model_type and
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
I have two models: Person and Relation. The second one stores information about relations
Let's say I have two models, Classes and People. A Class might have one
Suppose you have two models, User and City, joined by a third model CityPermission:
I'm having trouble with my HABTM relationship in CakePHP. I have two models like

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.