1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kukiwon
  • 1,222
  • 12
  • 20

1 Answers1

0

I would suggest using Aliases in your relationships which will help get your head around the data being returned.

For example, your User model could use SelectedPet and ProfilePicture in it's associations:

User.php model

/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
    'SelectedPet' => array(
        'className' => 'Pet',
        'foreignKey' => 'petid'
    ),
    'ProfilePicture' => array(
        'className' => 'Picture',
        'foreignKey' => 'picid',
    )
);

/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
    'Album' => array(
        'className' => 'Album',
        'foreignKey' => 'userid',
        'dependent' => false
    ),
    'Pet' => array(
        'className' => 'Pet',
        'foreignKey' => 'userid',
        'dependent' => false
    )
);

Your Pet model could use ProfilePicture as well:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'userid'
        ),
        'ProfilePicture' => array(
            'className' => 'Picture',
            'foreignKey' => 'picid'
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Album' => array(
            'className' => 'Album',
            'foreignKey' => 'petid',
            'dependent' => false
        )
    );

Picture model:

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Album' => array(
            'className' => 'Album',
            'foreignKey' => 'albumid'
        )
    );

..and finally your Album model:

/**
 * belongsTo associations
 *
 * @var array
 */
public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'userid'
    ),
    'Pet' => array(
        'className' => 'Pet',
        'foreignKey' => 'petid'
    )
);

/**
 * hasMany associations
 *
 * @var array
 */
public $hasMany = array(
    'Picture' => array(
        'className' => 'Picture',
        'foreignKey' => 'albumid',
        'dependent' => false
    )
);

With regards to the logic of an Album belonging to a User or a Pet, you could just handle this in your controller when saving data or returning it. I.e User is given preference over Pet.

I hope this helps.

Moz Morris
  • 6,681
  • 2
  • 20
  • 18