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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:04:37+00:00 2026-05-28T19:04:37+00:00

I have a ‘Complex’ model which hasMany Unit. My model associations are correct, everything

  • 0

I have a ‘Complex’ model which hasMany Unit. My model associations are correct, everything works, it’s just that I am having trouble getting a different model/association working correctly and it has made me wonder if I need to set this model up differently.

See, not ALL Unit will have a Complex associated with it. Some Unit are houses, some Unit are hotel rooms, and some Unit are just rental companies.

Because Complexes haveMany Units I am tempted to leave it alone, but I am wondering if I shouldn’t redefine the relationship as Complex belongingTo Unit. I am trying to not repeat myself in my database with adding in multiple instances of the same complex.

Here is my current Unit Model:

class Unit extends AppModel {
public $name='Unit';

public $belongsTo=array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user'
),
'Complex'=>array(
'className'=>'Complex',
'foreignKey'=>'complex_id'
)
);

public $hasOne=array(
    'Location'=>array(
        'className'=>'Location',
        'foreignKey'=>'location_id'
        )
    );

}

and here is my current Complex model:

class Complex extends AppModel {
    public $name='Complex';
    public $hasMany=array('Unit');
    public $hasOne=array(
        'Location'=>array(
            'className'=>'Location',
            'foreignKey'=>'location_id'
            )
        );






}

By the way, ‘Location’ is the model I am having trouble with (the Location part of my array currently returns empty when I call my unit/complex information in my view, so I need to get my association right).
If the Unit is a condo with a complex, I want it to return the Location of Complex. If it is a house or hotel or rental company, I want it to return the Location of Unit. I have to do several of these associations (images, location, and so forth) so I want to get everything straight right up front.

Is my current relationship between Unit and Complex correct or do I need to define everything differently?

UPDATE Here is my controller logic:

$this->paginate['Unit']=array(
        'limit'=>9,
        'order' => 'RAND()',
        'contain'=>array(
                'User'=>array(
                    'email'),
                'Complex'=>array(
                    'id','complex_name', 'realname', 'photo1','photo2','photo3','photo4','photo5'),
                'Location'=>array(
                    'complex_id','address', 'city','state','zip','area_code','exchange','sln','lat','lon','website')
                ),
        'conditions'=>array(
                'Unit.type'=>array('condo', 'rentalco'),
                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allcondos', $data);


}

And you can see the result I get here:

I am not currently interested in displaying a view of all Complexes, but the index page logic does have ContainableBehavior limited to just Complex(as originally I was going to have views for various Complexes):

 public function index() {
    $this->Complex->Behaviors->attach('Containable');
    $this->Complex->contain();
    $c=$this->Complex->find('all');
    $this->set('complexes', $c);
    }

UPDATE I have it working now, for anyone who encounters this problem. I had to change Location to the root and make Complex belongTo Location. Here are my models updated:

class Location extends AppModel {
    public $name='Location';
    public $hasMany=array('Complex', 'Unit');
    var $belongsTo=array(


    'Restaurant'=>array (
        'className'=>'Restaurant',
        'foreignKey'=>'restaurant_id'
        )
    );


}

class Complex extends AppModel {
    public $name='Complex';
    public $hasMany=array('Unit');
    public $hasOne=array('Image');
    public $belongsTo=array(
        'Location'=>array(
            'className'=>'Location',
            'foreignKey'=>'location_id'
            )
    );






}


class Unit extends AppModel {
public $name='Unit';

public $belongsTo=array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user'
),
'Complex'=>array(
'className'=>'Complex',
'foreignKey'=>'complex_id'
),

'Location'=>array (
        'className'=>'Location',
        'foreignKey'=>'location_id'
        ),
);

public $hasOne=array('Image');

}

Many thanks to Wylie for the help, this was a doozy!

  • 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-28T19:04:38+00:00Added an answer on May 28, 2026 at 7:04 pm

    http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html?containing-deeper-associations#containing-deeper-associations

    When using ‘fields’ and ‘contain’ options - be careful to include all foreign keys that your query directly or indirectly requires. Please also note that because Containable must to be attached to all models used in containment, you may consider attaching it to your AppModel.

    You can do that in your AppModel like this: public $actsAs = array('Containable');
    You don’t have to use Contain when you don’t want to so it shouldn’t get in the way.

    When a model A has model B, you use the foreign key within model B. So your foreign key on the Location should be complex_id. Although, that shouldn’t be so because not all units have a complex.

    It seems to me you should make the Location the “root”. So, Complex belongsTo Location and Unit belongsTo Complex and Location. Then Location hasOne/Many complex, etc. Either that or just put the address columns in the Unit/Complex tables.

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

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
Have just started using Google Chrome , and noticed in parts of our site,
Have had to write my first proper multithreaded coded recently, and realised just how
I have just tried to save a simple *.rtf file with some websites and
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a photography site that I want to prevent image copying from. How can
Have created a ATL COM project through which I am inserting Menu Items to
Have some dates in my local Oracle 11g database that are in this format:
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine

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.