Re: Associations in CakePHP with non-conventional database
usually you use two keys to create "flexible relations"
key "model" and key "foreign_id" (some call it foreign_key)
you just have to save array('model'=>'Pet', 'foreign_id'=>'1') or
array('model'=>'User', 'foreign_id'=>'3') etc with the rest of the
save data.
then you can easily query based on the model you are looking for
this way you can have albums for multiple models without creating
collisions.
if you need guidance check out some plugins from cakeDC like the tags
plugin or ratings plugin.
they do it this way to allow those plugins to work with all kinds of
models.
On 19 Nov., 05:07, Jordy van Kuijk <jvank...@gmail.com> wrote:
> We are converting an application for use with CakePHP 2.0.3. For some
> reason, I cannot seem to set proper relations between my models.
>
> Here's an example:
>
> User (id, petid, country, picid, ...)
> Pet (id, userid, picid, ...)
> Picture (id, albumid, ....)
> Album (id, userid, petid, ...)
> The meanings of these are the following: - A user can have multiple
> pets, but can only have selected one pet at the same time (therefore,
> petid in User)
>
> - Pets belong to one user
>
> - Pets and Users can have multiple pictures, but only one profile
> picture, therefore Pet.picid and User.picid
>
> - Pets and users can have multiple Albums
>
> I set up my models in CakePHP, but I cannot figure out which relations
> to use between them since the Database is not following the
> conventions. I've tried the following:
>
> User
> -> hasMany(Pets)
> -> hasOne(Picture)
> -> hasMany(Album)
>
> Pet
> -> belongsTo(User) (works fine, with foreignkey userid)
> -> hasMany(Album)
> -> hasOne(Picture)
>
> Album
> -> hasMany(Picture)
>
> ---- Logic to achieve this? It either belongs to a user or pet-----
>
> -> belongsTo(User)
> -> belongsTo(Pet)
>
> Picture
> -> belongsTo(Album)
> I'm new to CakePHP and cannot figure out the way to go here. Do you
> have any suggestions?
>
> Thanks
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
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
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home