zero juan
zero juan

Reputation: 199

How do I create a Unary association in CakePhp?

Is there a shortcut for creating Unary associations in Cake?

For example, a user is a friend of another user. I'm pretty sure it's going to violate cake's conventions if I try it the hard way and code the associations myself.

Cake has HABTM:

var $hasAndBelongsToMany = array(
    'User' =>
        array(
            'className' => 'User',
            'joinTable' => 'friends',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'friend_id',
            'unique' => true
        )
);

Will this work? A user model assoc to another user model.

Update: How do I save the data now?

$this->data["Friend"] = $request["Requester"];
$this->data["Admirer"] = $request["Requestee"];                         
$this->Friend->create();
$this->Friend->save($this->data);

$request["Requester"] and $request["Requestee"] both hold objects of type User. The HABTM rel is defined in the User model

var $hasAndBelongsToMany = array(
        'Friend' => array(
            'className' => 'Friend',
            'joinTable' => 'friends',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'id'
        ),
        'Admirer'=>array(
            'className'=>'Friend',
            'joinTable'=>'friends',
            'foreignKey' => 'friend_id',
            'associationForeignKey' => 'id',            
        )
    );

All that happens is the $data->["Friend"]'s id is stored in the id column of the friends table

Upvotes: 0

Views: 585

Answers (3)

TehTreag
TehTreag

Reputation: 66

Here is a solution I use in an example application

Two tables are needed, users and followers. The important fields are User->id, Follower->user_id, Follower->friend_id.

I hope this snippet helps.

<?php
class User extends AppModel {
    public $useTable='users';
    public $hasAndBelongsToMany=array(
        'Friend'=>array(
            'className'=>'User',
            'joinTable'=>'followers',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'friend_id',
            ),
        'Admirer'=>array(
            'className'=>'User',
            'joinTable'=>'followers',
            'associationForeignKey' => 'friend_id',
            'foreignKey' => 'user_id',
            ),
        );
}

-teh

Upvotes: 3

dogmatic69
dogmatic69

Reputation: 7585

Bjorn,

he is asking about User habtm User...

your join table should be users_users and the relation should look like this

    var $hasAndBelongsToMany = array(
        'Friend' =>
                array(
                        'className' => 'UserUser',
                        'joinTable' => 'users_users',
                        'foreignKey' => 'user_id',
                        'associationForeignKey' => 'friend_id',
                        'unique' => true
                )
);

i think that should do the trick

Upvotes: 1

Bjorn
Bjorn

Reputation: 5362

In CakePHP there are several variables in your model available to do that, like $hasMany, $belongsTo and $hasAndBelongsToMany. Please read the Cookbook for further explaination.. http://book.cakephp.org/view/78/Associations-Linking-Models-Together .

In your example, I think you need HABTM.

Upvotes: 0

Related Questions