Welcome Guest, Not a member yet? Register   Sign In
Merging/Updating 2 sets of Items with Datamapper
#1

[eluser]felix_[/eluser]
Hey there,
I've got a question which belongs to best practice for a common problem I think.

Let's say I've got a model producttyp, product and shop

Every product has got a producttype and a shop has many products and can offer different producttypes

Now I want to edit the products of a shop which belong to a special producttype

I show al list with all available products to this type and for each product I show a chechkbox which is preselected if the shop already offers this product.
Then i change the selections.

In my controller I get the list of id's with the new products.
What is the best way to update the relations between shop and products !?

Right now I manually create an array with the products I muste delete from the relation and one with the new ones.
Is there any automatic way to do something like this?
#2

[eluser]WanWizard[/eluser]
There is no automatic way.

What I usually do is to add a method to my model for dealing with this.

The method takes an array with id's that need to be related (which could be supplied by your checkboxes).It then deletes all relations from the relationship table using a standard $this->db->delete query, and creates the new relations using the array passed.

I use Innodb, and have encapsulated this in a transaction to avoid consistency issues.
#3

[eluser]felix_[/eluser]
If I delete all relations in general and then add the new ones didn't I produce a lot of overhead? I mean I delete for example 100 relations and then re add 90 of them.




Theme © iAndrew 2016 - Forum software by © MyBB