Remeber, has(One|Many) means "The other table points to me," and belongsTo means "I point to the other table." I would define the models something like this (assume fields basically same as you said):

User:
    hasMany Items

Item:
    belongsTo User
    hasMany Trades

Trade:
    belongsTo Item1
    belongsTo Item2

Personally, I would not put the users in the trades table, but that may make things more easy in the code, depending on your approach to some things.

CODE:

class User extends AppModel {
    var $name = 'User';
    var $hasMany = array( 'Item' => array( 'className' => 'Item' ) );
}

class Item extends AppModel {
    var $name = 'Item';
    var $belongsTo = array( 'User' => array( 'className' => 'User' ) );
    var $hasMany = array( 'Trade' => array( 'className' => 'Trade' ) );
}

class Trade extends AppModel {
    var $name = 'Trade';
    var $belongsTo = array( 'Item1' => array( 'className' => 'Item', 'foreignKey' => 'item1' ),
                            'Item2' => array( 'className' => 'Item', 'foreignKey' => 'item2' ));
}


In my experience (take with grain of salt), when retrieving Trade rows with find/findAll with a recursive setting of 3 (possibly 2?), you should have result rows that look something like this:

Array
(
    [Trade] => Array
       (
          ... normal trade fields ...
       )
    [Item1] => Array
       (
          ...item fields...
          [User] => Array
             (
                ...user fields...
             )
       )
    [Item2] => Array
       (
          ...item fields...
          [User] => Array
             (
                ...user fields...
             )
       )
)

Shane B wrote:
So I am no DBA, and this is my first CakePHP app, still learning the
ins and outs, maybe there is an easy way to do this..
Here is the setup:

Assume these psudo-tables:

Users
---------
id - pk
username
password
email
name

Items
---------
id - pk
user_id - fk
title
description
status

And the idea of the site is that people put items up, and can trade
them, so i'd like a trades table to keep track of live trades in
progress. So essentially I need:

Trades
----------
id - pk
item1
item2
user1
user2
status

HOWEVER, I'd like to use foreign keys instead of the item1 and 2 to
more easily work with the data and to allow it to bake correctly.  The
catch here though is that this would work perfectly with 1 item since
I could just associate using item_id, though I can't have TWO item_id
fields so how will I reference to unique items in the same table?
Also, correct me if I'm wrong but I probably don't even need user1 and
2 since they're associated in the Items table already.

Is there any easy way to do this in CakePHP so that bake should work?
I read but did not completely comprehend the $hasMany var in the Model
chapter of the manual, might this be the key to figuring this out?

Any help would be appreciated. I bring this up not only for this
project, but to learn how to do things RIGHT with CakePHP, for future
projects as well.

Thanks.



  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to