huzzah
huzzah

Reputation: 1805

Another "need to pull data from two tables in cakephp"

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 ...
)

Upvotes: 0

Views: 296

Answers (2)

Farray
Farray

Reputation: 8528

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.

Upvotes: 0

jwg2s
jwg2s

Reputation: 816

$this->Unit->find('all', array(
        'conditions' => array(
            'type' => 'condo',
            'active' => 1,
        ),
        'contain' => array(
            'User',
        ),
    ));

Use containable to get related data. You can use conditions within that contain as well to restrict what information you want.

http://book.cakephp.org/view/1323/Containable

Upvotes: 1

Related Questions