cakePHP HABTM, am I getting it all wrong?

依然范特西╮ 提交于 2019-12-12 02:43:07

问题


I understood that every new row, causes the deletion of the rows that were there before?

What is the idea behind it? I don't believe that it is ..
So, what am i getting wrong?


Edit A

I have a form that adds a store to the Stores table. the store have a column named owner_id which is associated to the Users table through a belongsTo relationship.

There is also a table named stores_users that supposed to store the mangers for each store, using the HABTM relationship.

For this table there is a form with an email field, that connects the user to the store by saving the record directly to the stores_users table.
So, there is no full HABTM save anywhere, if I understand the term correctly.

So, my questions are:

  1. Should I expect problems using it this way?
  2. Can you advice me about how to it, if my method is not the proper way?
  3. How can I use the stored data, using $this->User->find(...) to get all the stores that the user can manage?

回答1:


yes, thats the default behavior of HABTM in cakephp although this is not on "every row", but "every HABTM save".

this is working IF you always provide all HABTM values. and with baked views according to the specifications for such HABTM this is all working out of the box.

if you change the default behavior (old ones get not deleted) you will need to make sure that there are no duplicates. there are behaviors out there, I think, which try to accomplish that. but I would recommend for you to build your forms the way that the default behavior of cake can do its job.

Example:

IS: 1,3,6 (in DB for this key)

NEW: 2,3,6 (coming from form)

(cake deletes 1,3,6 and adds 2,3,6)

=> overall result (forgetting about different primary keys): "1" deleted, "2" added

so it might not be the most resource sparing way but its sure the easiest and fastest.



来源:https://stackoverflow.com/questions/8067475/cakephp-habtm-am-i-getting-it-all-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!