Welcome Guest, Not a member yet? Register   Sign In
[DataMapper] Saving several "many_to_many" relationships with same IDs
#1

[eluser]Ayumi[/eluser]
Hi and thank you if you can help me,

I have a raffle system in which database I have 2 tables : User and Gift

When a User buy a ticket for a Gift, a record is made in the gifts_users table.

When a User buy a second ticket, I want a NEW record with the same gift_id and user_id but a different date, and I don't know how to do this.

I'm doing this :

Code:
$gift->save($user);

but it does update the record with the new date instead of creating a new record.

How could I do this ?

Thanks ! ;-)
#2

[eluser]harshitha[/eluser]
you can use "date" in your gift_users table
#3

[eluser]Ayumi[/eluser]
[quote author="harshitha" date="1353822756"]you can use "date" in your gift_users table[/quote]

thank you for your answer, that's exactly what I want to do, I just don't know how to do it
#4

[eluser]WanWizard[/eluser]
You can't do it that way.

Your gifts_users table is the junction (or relationship) table between gifts and users, containing the foreign keys to link the two. From this it follows that the combination of both foreign keys has to be unique. Which means you can't insert a second record with the same keypair, it would break your database design.

From a database normalisation point of view, this will mean you'll have remove the date from the gifts_users table, and store it in a separate table, and create a one-to-many between gifts_users and giftuserdate (or something else).

Now, this structure will create some challenges, as it means you can't use a many-to-many relation between gifts and users anymore, but you'll have to create a model for gifts_users, and create a one-to-many between gifts_users_ and gifts, and between gifts_users_ and users. Now you can create a new relation between gifts_users_ and the new giftuserdate model.

This is possible, but it requires some work, and will require changes to your controller code as queries will change.

Alternatively, if the date is only a display item, you could work around it by storing the dates into an array, and serialize it when you store it in the join field. Or use some other trick to store multiple dates in a single field.
#5

[eluser]Ayumi[/eluser]
Damn !

Thank you for your answer Smile




Theme © iAndrew 2016 - Forum software by © MyBB