Hello,
For some reason I cannot figure out how to do the following.
Consider the scenario where there is a grid of Persons, and persons can be assigned Hobbies. However, multiple persons can share the same hobby, it is many-to-many.
I am using SmartGWT Pro/EE and using MySQL as a database.
I want the solution to look like this:
In the detailed view of One Person, there is a grid listing all the hobbies (Hobbies can be created in a different tab, so there's a read-only list here).
Beneath that, there is a grid, where hobbies can be dragged into. This way, one could assign hobbies to a person.
I worked it out partially, but not completely yet.
I think that in the classical way you would have tables like this:
Thus, I thought it would make sense to have 3 .ds.xml files:
Person:
Hobby:
Person2Hobby:
Firstly, is this the correct way? This avoids having redundant data in the Hobby2Model table by specifying the table name.
Now, when implementing the drag'n'drop, 3 things I cannot work out, maybe it's simple but I just don't see it :-).
1) How to make sure the person_id also gets set in Person2Hobby table? Now it is NULL. If i drop a hobby in the grid, the hobby_id IS set.
2) How to make sure that if I delete a selected hobby for a specific person (ie. person X stopped having 'volleyball' as a hobby), the hobby is not deleted from the Hobby table ? (this happens now).
3) If i delete a hobby altogether, how to update all the persons who have this hobby for not having this hobby anymore? I normally would do a foreign key in the database with ON DELETE CASCADE, is this settable in the .ds.xml? Or is this a 'manual sql process'?
Should I create operation-bindings for the other CRUD operations in this case? I actually don't really get why that should be there, can't it be inferred from the foreignKey statements in the Hobby2Model table?
Help appreciated! Since it is a pretty common use-case, I reckon there is a not too difficult solution for it (ie. not having to write complicated server-code, but let SmartGWT do the job?)
For some reason I cannot figure out how to do the following.
Consider the scenario where there is a grid of Persons, and persons can be assigned Hobbies. However, multiple persons can share the same hobby, it is many-to-many.
I am using SmartGWT Pro/EE and using MySQL as a database.
I want the solution to look like this:
In the detailed view of One Person, there is a grid listing all the hobbies (Hobbies can be created in a different tab, so there's a read-only list here).
Beneath that, there is a grid, where hobbies can be dragged into. This way, one could assign hobbies to a person.
I worked it out partially, but not completely yet.
I think that in the classical way you would have tables like this:
Code:
Persons(id,name) Hobbies(id,name) Person2Hobby(person_id,hobby_id).
Person:
Code:
<DataSource ID="Persons" serverType="sql" tableName="Persons"> <fields> <field name="id" type="sequence" hidden="true" primaryKey="true"/> <field name="name" title="Name" type="text" length="128" /> </fields> </DataSource>
Code:
<DataSource ID="Hobbies" serverType="sql" tableName="Hobbies"> <fields> <field name="id" type="sequence" hidden="true" primaryKey="true"/> <field name="name" title="Name" type="text" length="128" /> </fields> </DataSource>
Code:
<DataSource ID="Person2Hobby" serverType="sql" tableName="Person2Hobby"> <fields> <field name="id" type="sequence" primaryKey="true" hidden="true"/> <field name="person_id" type="integer" foreignKey="person.id"/> <field name="hobby_id" type="integer" foreignKey="hobby.id"/> <field name="name" type="text" tableName="Hobbies" customSQL="true"/> </fields> <operationBindings> <operationBinding operationType="fetch" customValueFields="name" customCriteriaFields="name"> <tableClause>Person2Hobby, Hobbies</tableClause> <whereClause>Hobbies.id = Person2Hobby.hobby_id AND ($defaultWhereClause)</whereClause> </operationBinding> </operationBindings> </DataSource>
Now, when implementing the drag'n'drop, 3 things I cannot work out, maybe it's simple but I just don't see it :-).
1) How to make sure the person_id also gets set in Person2Hobby table? Now it is NULL. If i drop a hobby in the grid, the hobby_id IS set.
2) How to make sure that if I delete a selected hobby for a specific person (ie. person X stopped having 'volleyball' as a hobby), the hobby is not deleted from the Hobby table ? (this happens now).
3) If i delete a hobby altogether, how to update all the persons who have this hobby for not having this hobby anymore? I normally would do a foreign key in the database with ON DELETE CASCADE, is this settable in the .ds.xml? Or is this a 'manual sql process'?
Should I create operation-bindings for the other CRUD operations in this case? I actually don't really get why that should be there, can't it be inferred from the foreignKey statements in the Hobby2Model table?
Help appreciated! Since it is a pretty common use-case, I reckon there is a not too difficult solution for it (ie. not having to write complicated server-code, but let SmartGWT do the job?)
Comment