Closed chronick closed 10 years ago
Here is an article detailing HABTM relationships in CakePHP: http://jamnite.blogspot.com/2009/03/how-to-paginate-self-referential-habtm.html
Hey I have a question about saving the associated models:
if you look at my branch, you will see three methods in the User model: one for reading, creating, and removing people from the hotlist, respectively.
I'm having trouble with the addToHotlist method. Here it is:
public function addToHotlist($user_id, $friend_id) {
CakeLog::write('HOTLIST', 'Entered addToHotlist(' . $user_id . ',' . $friend_id . ')');
$this->Hotlist->save(array('user_id' => $user_id, 'friend_id' => $friend_id));
return $this->getHotlist($user_id);
}
This seems to create three rows instead of the one that it should (detailed in the screenshot below, don't mind the first row):
What am I doing wrong, do you think?
Oh yes, you can find the rest of my changes so far in https://github.com/TimKJones/CribSpot/tree/sharing
My initial thought is that when you're saving a row, the first index in the array needs to be the name of the table, so in this example, it would look like this: $this->Hotlist->save(array('Hotlist' => array('user_id' => $user_id, 'friend_id' => $friend_id)));
Try that and let me know if it works. If it doesn't, I'll look into it further.
On Wed, Nov 6, 2013 at 1:50 PM, Nick Donohue notifications@github.comwrote:
Hey I have a question about saving the associated models:
if you look at my branch, you will see three methods in the User model: one for reading, creating, and removing people from the hotlist, respectively.
I'm having trouble with the addToHotlist method. Here it is:
public function addToHotlist($user_id, $friend_id) { CakeLog::write('HOTLIST', 'Entered addToHotlist(' . $user_id . ',' . $friend_id . ')'); $this->Hotlist->save(array('user_id' => $user_id, 'friend_id' => $friend_id)); return $this->getHotlist($user_id);}
This seems to create three rows instead of the one that it should (detailed in the screenshot below, don't mind the first row): [image: screenshot 2013-11-06 13 47 02]https://f.cloud.github.com/assets/481092/1485386/1f24d772-4714-11e3-9325-cecb461dab1d.png
What am I doing wrong, do you think?
— Reply to this email directly or view it on GitHubhttps://github.com/TimKJones/CribSpot/issues/167#issuecomment-27901753 .
Is it the array of the table proper or the name of the association alias?
For example, the association at the top of the model is named Hotlist
, while the table in MySQL is named users_friends
.
Well I'm a bit closer:
$this->Hotlist->save(array('users_friends' => array('user_id' => $user_id, 'friend_id' => $friend_id)));
I have the $user_id output to a log and it is definitely correct, but it doesn't seem to want to head into the database. What do you think?
screw it, I just did a manual sql query:
$this->Hotlist->query("insert into users_friends (user_id, friend_id) values($user_id, $friend_id);");
We can get back to this later.
Sorry for not getting back. Well have to get rid of the insert but I'll investigate a bit later On Nov 6, 2013 3:07 PM, "Nick Donohue" notifications@github.com wrote:
screw it, I just did a manual sql query:
$this->Hotlist->query("insert into users_friends (user_id, friend_id) values($user_id, $friend_id);");
We can get back to this later.
— Reply to this email directly or view it on GitHubhttps://github.com/TimKJones/CribSpot/issues/167#issuecomment-27908748 .
I decided to attempt a join model, called Friendship, that works with the users_friends table directly. Either the $hasAndBelongsToMany stuff is not that mature in cake v2.2, or I'm just trying to do things way off the mark.
OK I think that should be good. I was in a presentation and now we have to give a presentation so haven't had a chance to dig into the code unfortunately. I'll take a look as soon as I can. On Nov 6, 2013 4:12 PM, "Nick Donohue" notifications@github.com wrote:
I decided to attempt a join model, called Friendship, that works with the users_friends table directly. Either the $hasAndBelongsToMany stuff is not that mature in cake v2.2, or I'm just trying to do things way off the mark.
— Reply to this email directly or view it on GitHubhttps://github.com/TimKJones/CribSpot/issues/167#issuecomment-27913876 .
I looked at it some more, and without having implemented the hasandbelongstomany relationship before, it's hard for me to really debug what was the issue. If adding the friendship table seems to work, and you're comfortable with it from a design standpoint, then I think that should be fine.
I tried doing that as well, but had quite a few issues with that Join Model approach, namely that when doing a $this->find('all', ...)
in the model, the call would recurse through each user and its friends indefinitely, thus killing the memory, I attempted to use other arguments to the find()
method, namely 'list'
, but that only returned the Friendship records, and not the associated users that I needed. I also attempted to set the $recursive
property on the Users model to different values, with no success.
Anyway, I decided to go back to listing the Hotlist via the $hasAndBelongsToMany
, and updating it with straight SQL just so we can keep from getting stuck in framework trivialities. This seems to work effectively, barring any sql injection concerns, but there also seems to be a Sanitize
class in Cake that could be used to process the input, if it became necessary to use the queries in production.
On another note, I'm going to get started on the frontend stuff for adding and removing friends - @edance I remember we discussed briefly the build process for the coffeescript/LESS - could you send me your workflow when you get a chance?
Sorry for not getting back to you we slept in after working really late last night. For building coffee, we just compile specifically the files we've changed with a command like "coffee -c -o ../js/src/ filename.coffee" from within the coffee folder. I could be wrong about less, but for now, I believe there is no pre-compilation done - it just gets compiled at run time On Nov 7, 2013 11:10 AM, "Nick Donohue" notifications@github.com wrote:
On another note, I'm going to get started on the frontend stuff for adding and removing friends - @edance https://github.com/edance I remember we discussed briefly the build process for the coffeescript/LESS - could you send me your workflow when you get a chance?
— Reply to this email directly or view it on GitHubhttps://github.com/TimKJones/CribSpot/issues/167#issuecomment-27979184 .
Thats cool, sounds good thanks. Right now I'm getting the scaffolding worked out in js, my goal by tomorrow is to be able to list all hotlisted friends, remove and (possibly) add them with AJAX by tomorrow. right now I have it working with an html form that you can access by /friends/hotlist after you set up the database with the following schema:
(table users_friends)
id PRIMARY KEY A_I
user_id INT
friend_Id INT
hotlist BOOL
created DATETIME
updated DATETIME
Just a quick FYI - While working with the typeahead, it is plain that it does not offer the level of customizability we need for this feature. I'm going to go ahead and get it working with https://github.com/twitter/typeahead.js/ which is a separate thing (and has replaced Bootstraps typeahead in 3.0). This allows us to pull the autocomplete from multiple sources and provides the callbacks we need to override.
Lets make this the discussion centerpoint for adding Friends, hotlists, and the related sharing/invites feature for now.