문제

I have two models, Posts and Tags. Posts HasAndBelongsToMany Tags.

Let's say a Post 1 has the tags world, news, and paper.

Now in the joining table, I want to remove the association between the Tag "paper" and Post 1, but the Tag "paper" should not be deleted from the tags table. Only the association in the joining table should be deleted.

How do I do this in CakePHP?

도움이 되었습니까?

해결책

When you do any HABTM operation other than adding a new one, Cake deletes and recreates the associated join table rows. All you have to do is get the record, remove the tag and save the record again.

In the view I write the tags into one form input field. When the record is saved, I process the value of this field to extract the tags (explode by comma or space or whatever) then save the tags.

There is a good guide here: http://mrphp.com.au/code/working-habtm-form-data-cakephp

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top