private function setRelatedObjectCollections($object) { $this_table_name = get_class($object); //get the mapping tables $mapping_tables = DataLayer::getMappingTables($object, $this->conn); foreach ($mapping_tables as $mapping_table_name) { $this_table_fk_col_name = ''; $related_table_name = ''; $related_table_fk_col_name = ''; //from each mapping table, get the related table info $relatedTablesInfo = DataLayer::getRelatedTablesInfo($object, $mapping_table_name, $this->conn); $this_table_name = $relatedTablesInfo['this_table_name']; $this_table_fk_col_name = $relatedTablesInfo['this_table_fk_col_name']; $related_table_name = $relatedTablesInfo['related_table_name']; $related_table_fk_col_name = $relatedTablesInfo['related_table_fk_col_name']; if ($related_table_name) { //now there's no way to know from the current related ids if the user has deleted a related object //so right now the only way we can think of to remove all entries for the main table id $clear_relations = "delete from {$mapping_table_name} where {$this_table_fk_col_name} = {$object->id};"; if ($this->conn->query($clear_relations)) { //then re-insert the relations. for all relations foreach ($object->{$related_table_name} as $relatedObject) { $update_relations = "insert into {$mapping_table_name} ({$this_table_fk_col_name},{$related_table_fk_col_name}) values({$object->id},{$relatedObject->id});"; if ($this->conn->query($update_relations)) { } else { throw new Exception($this->conn->error, $this->conn->errno); } } } else { throw new Exception($this->conn->error, $this->conn->errno); } } } }