Monday, March 2, 2009

Re: 2 foreign keys to same model?

On Mar 1, 9:46 am, "sjunwan...@gmail.com" <sjunwan...@gmail.com>
wrote:
> Hi, cake1.2 here,
>
> I have a question on how to set this database up by cake's convention.
> I have the following 2 tables:
>
> users
> assets
>
> each user has many assets, but users can allow other users to access
> their assets
> so I figure the following, a third table is needed:
>
> a table that has attributes:
> owner, allowed
>
> they both point to users_id, how do I make this table model in
> cakephp? Is there another way to do this?
> Because i know users_id means foreign key to table users, but how do
> you have 2 foreign to the same table?
>
> thank you

First relationship is a simple one to many relation:
- User -> is owner of -> Asset.

Second relationship is a many to many relation:
- User -> Can access -> Asset
- Asset -> Is accessible by -> User
Thus the second relationship requires a entity in between User and
Asset, for example named AssetUsers.

Create one model for each of the entities in the first relationship.
No problem there I hope :)

Depending on which way you wish to present the second relationship,
create either a model going from Asset to Users through AssetUsers or
vice verse.

Enjoy,
John

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: