CakePHP HABTM - I don't understand it

妖精的绣舞 提交于 2019-12-22 14:48:10

问题


Asked before but still in the dark.

cakePHP HABTM, am I getting it all wrong?

Will try again:
I have two tables: stores and managers
I want to create a new table, managers_stores and use it to keep a complete list of mangers for each store.
I understood that each save() action that will provide the full record data (as I intend to do) will result in the deletion of the table rows and only the new record will be kept..
This is an UNDESIRED behavior for my needs. So, how should I manage the HABTM info of my application and what's the idea behind deleting data that I saved?!

thanks


回答1:


save() will not delete anything, only saving with saveAll() will truncate the managers_stores entries for the particular model you're saving, if the unique value of the relationship is true. This is not made entirely clear in the manual:

unique: If true (default value) cake will first delete existing relationship records in the foreign keys table before inserting new ones, when updating a record. So existing associations need to be passed again when updating.

http://book.cakephp.org/1.3/en/view/1044/hasAndBelongsToMany-HABTM

Whenever you save() an individual record, only that individual record will be touched.
When you saveAll() a, say, Manager record with associated Store records in the same go, the managers_stores links will be reset and only the Stores that were in the record you saved will be associated with the Manager. E.g. (in pseudo code):

  • Manager HABTM stores 1, 2 and 3
  • Manager->saveAll(array('Manager' => ..., 'Store' => array(4, 5, 6)))
  • Manager now HABTM stores 4, 5 and 6; the existing associations were overwritten

So, either set the unique value in the association to false if you don't like this behavior, or manage the HABTM records manually. To be quite honest, for any halfway complex application, relying on Cake's automagic handling of HABTM relationships can get quite fragile and hard to debug, so I always manage HABTM records myself.

$this->Manager->save(...);
$this->Manager->ManagersStore->save(array('manager_id' => $this->Manager->id,
                                          'store_id'   => 42));



回答2:


'Has and Belongs to many' is where the objects are loosely tied together. If you used the stores and managers analogy, one store has many managers, and one manager has many stores (not sure how that works, maybe they are roving managers? :P).

Firstly I think the example you use with managers stores isn't HABTM. It's just one store has many managers, not many stores. Using HABTM here would imply that managers can belong to multiple stores at the same time! However I will explain anyway.

Anyhow, when using HABTM, this relationship requires an intermediate table probably called manager_stores or something similar. This would be set up automatically by CakePHP if using the automated tools. This lets you link an unlimited number of managers to an unlimited number of stores.

What happens when you update and delete records depends on the configuration of your MySQL Relationships (you need to use the InnoDB engine).

This is regardless of what relationship type you are using.

If you are using phpMyAdmin, go into the structure view of your table. Then click 'relation view'.

If you are set up with InnoDB correctly you should see the relations and two columns saying "ON UPDATE" and "ON DELETE". This determines the behavior when you modify records.

I would be here all day explaining the different types of ON DELETE and ON UPDATE actions. Try doing some research into relational database queries!

I hope this helps you a bit. I will happily answer any questions you have!



来源:https://stackoverflow.com/questions/8773842/cakephp-habtm-i-dont-understand-it

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