1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 |
<?php
/*
A user can add other users as family or friend, or block another user.
my friends table looks very much like families table and blocks table,
with (user_id, friend_id) or (user_id, family_id) or (user_id, block_id) as keys
*/
/*
The strange part is when I try to save the habtm relationship, like when i try to do:
*/
$this->data['User']['id'] = 1;
$this->data['HasAsFriend']['HasAsFriend']['friend_id'] = 2;
$this->User->save($this->data);
/*
Then I'll have entries inserted into not only my friends table, but also my families table and blocks table. How is that possible?
*/
/* my associations in User model */
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'conditions' => '',
),
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'friend_id',
'associationForeighKey' => 'user_id',
'conditions' => '',
),
'className' => 'User',
'joinTable' => 'families',
'foreignKey' => 'user_id',
'associationForeignKey' => 'family_id',
'conditions' => '',
),
'className' => 'User',
'joinTable' => 'families',
'foreignKey' => 'family_id',
'associationForeighKey' => 'user_id',
'conditions' => '',
),
'className' => 'User',
'joinTable' => 'blocks',
'foreignKey' => 'user_id',
'associationForeignKey' => 'block_id',
'order' => 'Block.created DESC',
),
);
/*
and even if I try the behavior at http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior
$this->User->habtmAdd('HasAsFriend', 1, 2);
returns the same result as inserting data in other tables as well.
I have tried both unbindModel() and unbindAll() (provided by the behavior),
and verifying the unbind results after unbind calls.
$this->User->unbindAll(array('hasAndBelongsToMany' => array('HasAsFriend')));
$this->log("current habtm: " . print_r($this->User->hasAndBelongsToMany, true));
The error log reads:
2008-05-08 17:03:49 Error: current habtm: Array
(
[HasAsFriend] => Array
(
[className] => User
[joinTable] => friends
[foreignKey] => user_id
[associationForeignKey] => friend_id
[uniq] =>
[conditions] =>
[with] => Friend
[fields] =>
[order] =>
[limit] =>
[offset] =>
[unique] => 1
[finderQuery] =>
[deleteQuery] =>
[insertQuery] =>
)
)
Which indicates that those relationships are unbound successfully.
But the habtmAdd still inserts into other tables:
$this->User->habtmAdd('HasAsFriend', 1, 3);
will result with SQL queries:
28 INSERT INTO `friends` (`user_id`,`friend_id`) VALUES (1,3)
29 SELECT `Friend`.`id` FROM `friends` AS `Friend` WHERE `friend_id` =
1
30 INSERT INTO `friends` (`friend_id`,`user_id`) VALUES (1,3)
31 SELECT `Family`.`id` FROM `families` AS `Family` WHERE `user_id` =
1
32 INSERT INTO `families` (`user_id`,`family_id`) VALUES (1,3)
33 SELECT `Family`.`id` FROM `families` AS `Family` WHERE `family_id`
= 1
34 INSERT INTO `families` (`family_id`,`user_id`) VALUES (1,3)
35 SELECT `Block`.`id` FROM `blocks` AS `Block` WHERE `user_id` = 1
36 INSERT INTO `blocks` (`user_id`,`block_id`) VALUES (1,3)
This is wierd. Is this a bug in cake or is it my misconfiguration in
User model?
*/
?> |
