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

  • Home
  • SEARCH
  • 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 6929773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:21:38+00:00 2026-05-27T11:21:38+00:00

Ok, I have two tables, users and units. ‘Unit’ belongsTo ‘User’, and ‘User’ hasMany

  • 0

Ok, I have two tables, users and units. ‘Unit’ belongsTo ‘User’, and ‘User’ hasMany ‘Unit’. I would like to access all of the data from my units table, and append SOME of the data from my users table for the view. Here is my controller code:

public function condos() {
    $u=$this->User->Unit->find('all', array('conditions'=>array('type'=>'condo',     'active'=>1)));
    $this->set('allcondos', $u);
}

I have two models, User and Unit, here is the code for them:
from unit.php:

class Unit extends AppModel {
    var $name='Unit';
    var $belongsTo=array(
        'User'=>array(
        'className'=>'User',
        'foreignKey'=>'user_id'
    ) );
}

and from user.php:

class User extends AppModel {
    var $name='User';
    var $hasMany=array(
        'Unit'=>array(
        'className'=>'Unit',
        'foreignKey'=>'user_id'
    ) );
}

Here is my view code:

<?php foreach ($allcondos as $condo) { ?>

<section class="listings">
<figure>
<img src="<?php echo $condo['Unit']['photo1']; ?>" />
</figure>
<h1><?php echo $condo['Unit']['complex'], ' ', $condo['Unit']['unitnum']; ?></h1>
<h2><?php echo $condo['Unit']['city']; ?>, <?php echo $condo['Unit']['state']; ?> | (<?php echo $condo['User']['area_code']; ?>)<?php echo $condo['User']['exchange'];?>-<?php echo $condo['User']['sln'];?></h2>
<p><?php echo $condo['Unit']['unit_desc']; ?></p>
</section>
<?php } ?>

I have a problem with the call to $condo[‘User’][‘any_field’] as it returns zero ‘User’ data. (It returns the ‘Unit’ data just fine). I don’t know how to access both tables, obviously.

If I debug $allcondos in the view, it looks like this:

Array
(
    [0] => Array
    (
        [Unit] => Array
            (
                [id] => 1
                [user_id] => caribeincrentals
                [additional fields] ...
            )

        [User] => Array
            (
                [id] => 
                [user_id] => 
                [additional fields] ...
            )

    )
    [1] => Array ...
)
  • 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-27T11:21:39+00:00Added an answer on May 27, 2026 at 11:21 am

    Without your database details, it is tough to say exactly what is going on. However, you seem to be using a string value as the foreignKey that links your 2 models together. This is a bad idea.

    I suggest changing the user_id fk to an int datatype that references the id key column on the Users table (well, assuming that id is an autoincrementing integer value).

    It’s tough to say if this is the exact problem, but it’s definitely worth checking out and will help you avoid some other headaches in the future.

    UPDATE

    If you structure your tables according to Cake conventions, most of the associative work will be done for you. Here’s how I would do it:

    Users table (‘users’):

    id        primary key (int)
    

    User model:

    $hasMany = array( 'Unit' );
    

    Units table (‘units’):

    id        primary key (int)
    user_id   corresponds to id in users table
    

    Unit model:

    $belongsTo = array( 'User' );
    

    If you set your tables & models up like that, Cake will automatically associate ‘user_id’ in the units table with ‘id’ in the users table.

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

Sidebar

Related Questions

I have two tables named users and contacts . I would like to delete
I have two tables users(id, username, password) and user_badges(user_id, badge_id, badge_slot) I would like
I have two tables, users and hra. A user can only have one record
I have two tables: Users and Roles. A user may have more roles, so
I have two tables users registered_members I want to confirm values from user table
If I have two tables: Users and Appointments. How would I query the db
I have two tables: Users and Profiles. A user may have a profile and
I have two tables Users and Companies: public class User { // Properties public
In my application I have two tables: Users and Profiles. A user has a
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want

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.