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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:42:18+00:00 2026-05-19T01:42:18+00:00

Suppose (for lack of better example) I have a model Person and another model

  • 0

Suppose (for lack of better example) I have a model Person and another model Twin (meaning a pair of twins). Twins has two Person foreign keys, say first_born_id and second_born_id referencing the id fields of two different people (in Person). How do I set up the relationships in cake?

I guess Twin would have something like:

$belongsTo = array('FirstBorn' => array('className' => 'Person',
                                        'foreignKey' => 'firstborn_id'),
                   'SecondBorn' => array('className' => 'Person',
                                         'foreignKey' => 'secondborn_id'));

But how should I set up Person? I could do it like:

$hasOne = array('TwinAsFirstborn' => array('className' => 'Twin',
                                           'foreignKey' => 'firstborn_id'),
                'TwinAsSecondborn' => array('className' => 'Twin',
                                           'foreignKey' => 'secondborn_id'));

But then when I have a Person and I want to know about its twin I would need to check both relationships. I guess I am hoping there is a way to get a “Twin” relationship in Person representing any of the two ways a Person can be in a Twin relationship.

Or is there a better way to set this up?

  • 1 1 Answer
  • 1 View
  • 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-19T01:42:19+00:00Added an answer on May 19, 2026 at 1:42 am

    I agree with the Twin example being a little confusing. Let me assume that you have a Product model (Twin in your example) and it always has 2 Component models attached to it.

    components: id - name
    products: id - component1_id - component2_id
    

    I would set up Product as follows:

    var $belongsTo = array(
        'Component1' => array(
            'className' => 'Component',
            'foreignKey' => 'component1_id'
        ),
        'Component2' => array(
             'className' => 'Component',
             'foreignKey' => 'component2_id'
        )
    );
    

    And Component as:

    var $hasMany = array(
        'ProductWithComponentAsComponent1' => array(
            'className' => 'Product',
            'foreignKey' => 'component1_id'
        ),
        'ProductWithComponentAsComponent2' => array(
            'className' => 'Product',
            'foreignKey' => 'component2_id'
        )
    );
    

    Basically, you should replace your hasOne with hasMany. Each component hasMany products where it is the first component. At the same time, it hasMany products where it is the second component. Hope that makes things clear.

    EDIT 1: (Oh, and “ProductWithComponentAsComponent#” is just for the purpose of explanation. You can keep whatever short, sweet alias you’d like depending on your actual models.)

    EDIT 2: Simple rule of thumb for using hasOne relationships – Use it only when you’re splitting a single table into many (like User/Profile)

    EDIT 3: If you want all Products for a Component, then you can do this in two ways.

    (A) Define the foreignKey as false in your hasMany relation.

    var $hasMany = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => false,
            'conditions' => array(
                'or' => array(
                    "Product.component1_id = Component.id",
                    "Product.component2_id = Component.id"
                )
            )
        )
    );
    

    (B) If the above method does not work (Cake acts weird now and then), you can also use a join to make it obey. Create a function in the Component model like so:

    function fetchProducts($id) {
        if (!$id) {
            return null;
        }
        return $this->Product->find('all', array(
            'fields' => array('Product.*'),
            'joins' => array(
                array(
                    'table' => 'components',
                    'alias' => 'Component',
                    'foreignKey' => false,
                    'type' => 'inner',
                    'conditions' => array(
                        'or' => array(
                            "Product.component1_id = Component.id",
                            "Product.component2_id = Component.id"
                        ),
                        'Component.id' => $id
                    )
                )
            )
        ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have two tables, - emp(empId number(1),empName varchar2(50)) and - manager(manId number(5),managerName varchar2(100))
Suppose I have three tables (Patients Doctors and Medicines). The Patients table has a
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a process that is updating a record and encounters a record

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.