开发者

Merge entries in CakePHP

开发者 https://www.devze.com 2022-12-28 06:30 出处:网络
Let\'s say I have a Model, for example User, and I want to merge two instances of this Model, say merge User2 into User1. Explicitly this is what I mean:

Let's say I have a Model, for example User, and I want to merge two instances of this Model, say merge User2 into User1. Explicitly this is what I mean:

  1. 开发者_如何转开发

    If a field is already filled in User1, it should remain the same

  2. If a field is missing in User1 but is present in User2, it should be copied

  3. If SomeModel BelongsTo User, every instance of SomeModel pointing to User2 should be modified to point to User1

  4. Same if SomeModel HasAndBelongsToMany User

  5. If SomeModel HasMany User, and SomeModel1 Has User2 but no other instance Has User1, it should be modified so that SomeModel1 has User1 instead

  6. If SomeModel HasMany User, SomeModel1 Has User1 and SomeModel2 Has User2... well, I'm not sure here, I guess the only solution is to discard SomeModel2, since User1 can BelongTo only one SomeModel.

Finally User2 should be removed.

Is there a way to automate this? Maybe a Behaviour? If not, I may consider creating it, since I will need it a lot.


We made a Merge behaviour exactly for this purpose back in 2008.

It may require some tweaking to work correctly with the latest CakePHP stable release but you can find it here

0

精彩评论

暂无评论...
验证码 换一张
取 消