0

I have a hasAndBelongsToMany relation between Post and User called "Subscriptions". (instead of posts_users) It contains the user_id and the post_id.

I have it working but when i update a field of the Post model, then the record is deleted from the Subscriptions table. Why is this happening?

The update is this (at my Post model):

public function markAsRead(){       
    $this->read(null, $this->id);
    $this->set('user_read', 1);

    return $this->save();
}

Thanks.

Alvaro
  • 40,778
  • 30
  • 164
  • 336

1 Answers1

-2

Please check the online documentation because I suspect you're perhaps doing it the wrong way?

Sam Delaney
  • 1,305
  • 11
  • 10
  • I think i did it in the right way. This is what CakePHP documentation says: "By default when saving a HasAndBelongsToMany relationship, Cake will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12." So... i finally transformed it into a two BelongsTo and one hashMany. – Alvaro Oct 17 '12 at 16:10
  • That is true, but I think you have to ensure that the original rows on the join table are in the same set of data you're trying to save. e.g. read the rows first, append the new ones, and then save – Sam Delaney Oct 17 '12 at 17:07
  • I see what you mean. Anyway, i found it more simple to create other kind of relations. Thanks anyway. – Alvaro Oct 18 '12 at 11:15