2

I am new to CakePHP so please be easy on me.

I have this issue that I don't seem to be able to solve. I have a "contact" table and would like this table to have a many to many relationship with itself. The objective is to show that a contact is related to other contacts from the same company. So when clicking a contact's profile, I will be able to view other contacts that are related to that particular person.

I have no idea how to approach this issue. I'm thinking of a join table but doesn't seem to be possible? or I might be wrong. If anyone has any idea how to do this, please leave as much detail as possible so that I can understand how to do this (as I'm new to CakePHP as stated earlier).

Thanks!!

Edit: Tried the method stated by User996302 but still not working.

This is a portion of the Contact model for the relationship:

public $hasAndBelongsToMany = array( 'Relation' => array( 'className' => 'Contact', 'joinTable' => 'contacts_contacts', 'foreignKey' => 'contact_id', 'associationForeignKey' => 'related_id', 'unique' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' ) ); 

I am using the saveAll function in the controller. In the add section of the controller, I have this at the end:

$relations = $this->Contact->Relation->find('list');     $this->set(compact('relations'));

In the add view page I have this line of code to display the selection box:

echo $this->Form->input('Relation', array('type'=>'select', 'multiple' => true));

I have a contacts and a contacts_contacts table. The contacts_contacts table has three attributes: id, contact_id, related_id.

I have done all these but the selection list in the form does not display any contact that I can choose from. Not sure where the problem is. Any help will be great!

1 Answer 1

0

Those links should help you: example, SO question on the question.

Edited to correct a mistake.

Sign up to request clarification or add additional context in comments.

8 Comments

Both linked examples do use a join table. Still, they are a good solution to the question.
You're right. Made a confusion because you don't see the join table in cakephp unless it's a HABTM through.
I've tried the method in the examples but still not working. Maybe I'm doing something wrong....I'm not sure. This is a portion of the Contact model for the relationship: public $hasAndBelongsToMany = array( 'Relation' => array( 'className' => 'Contact', 'joinTable' => 'contacts_contacts', 'foreignKey' => 'contact_id', 'associationForeignKey' => 'contact_id2', 'unique' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' ) );
In the controller, I'm using saveAll and have this: $relations = $this->Contact->Relation->find('list'); $this->set(compact('relations')); In the add view page: echo $this->Form->input('Relation', array('type'=>'select', 'multiple' => true)); I have a contacts table and a contacts_contacts join table. The contacts_contacts table has three attributes: id, contact_id, contact_id2 Am I missing something here? The list to choose the contacts to link them is not displaying at all in the form.
'associationForeignKey' => 'contact_id2' is wrong. It must be of the form something_id. No 2. Same in the table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.