I have a grails project with a class that I can delete no problem when doing it "manually" from the controller. I use the following code.
def delete = {
def projectInstance = Project.get( params.id )
def employee = projectInstance.employee
def projectarray = new ArrayList<Project>();
projectarray += employee.getProjects()
println("Size of projectarray is " + projectarray.size())
if(projectInstance) {
def rolearray = []
projectarray.remove(projectInstance)
def temp = new TreeSet<Project>();
temp += employee.getProjects()
temp.clear()
temp.addAll(projectarray)
employee.projects = temp
projectInstance.employer = null
projectInstance.delete(flush:true)
flash.message = "Project ${params.id} deleted"
redirect(action:"edit", controller: "employee", id: employee.id)
}
else {
flash.message = "Project not found with id ${params.id}"
redirect(action:list)
}
}
So that deletes a single instance fine.
Now i want to, from a different controller, remove ALL projects from an employee.
This is stored in the employee like so:
class Employee implements Comparable
{
static hasMany = [projects:Project]
static constraints =
{
}
static mapping = {
projects cascade:"all-delete-orphan", lazy:false
}
@XmlElementWrapper(name="projectslist")
SortedSet<Project> projects = new TreeSet<Project>(); // make a sortedSet?
}
So how would I now delete all projects from a particular employee instance?