Delete all children of a parent (when saving)

Posts   
 
    
azagthoth
User
Posts: 2
Joined: 31-Jul-2008
# Posted on: 31-Jul-2008 07:06:57   

Hi All,

I have a 2 tables with a 1:n relationship.

Call it :

1 Student : n Languages

What I am doing loading the Student record from the DB, and then manually adding the Languages collection in memory (one by one in code).

I would now like to save the Student and the Language collection. This works nicely with a Student.save(true).

However, if records (same key) exist in the Languages table that are loaded into the Languages collection I get a PK violation.

I suppose my question is, is there a way to remove all records from a child table given a partial key [ie the primary key of the parent]?

Rob

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 31-Jul-2008 09:29:28   

You can use DeleteMulti() in selfServicing or DeleteEntitiesDirectly() in Adapter. Both accept a filter.

azagthoth
User
Posts: 2
Joined: 31-Jul-2008
# Posted on: 31-Jul-2008 12:15:23   

Walaa wrote:

You can use DeleteMulti() in selfServicing or DeleteEntitiesDirectly() in Adapter. Both accept a filter.

Thanks, but does this then lock you into removing only those child objects in the collection (or a filtered version of them?). Sorry for the stupid question - still learning the architecture here.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 31-Jul-2008 15:11:27   

DeleteMulti(), if called without passing a filtering parameter, it will delete all entities in the collection, but if called with a filter, then it will Delete from the database all entities of the type this collection is for which match with the specified filter.

DeleteEntitiesDirectly(), works directly on the database.