V7.4.3 building a Back Office "Relations Manager" plugin which I hope to release as a package. The very last step is to update the umbracoRelation table. I have the query which works when run independently. I can call the controller, but I can't quite get the syntax for the db to actually update. Any nudge in the right direction would be appreciated. Here's what I have atm which fails at db.Update(query);:-
// Update the relevant site's parentId
public bool SaveSite(int newParentId, int siteId) {
//get the database
var db = UmbracoContext.Application.DatabaseContext.Database;
//build a query to update the umbracoRelation table
var query = "UPDATE umbracoRelation SET parentId = '" + newParentId + "' WHERE childId = '" + siteId + "'";
//update data in DB with the query and return true if complete
try {
db.Update(query);
return true;
} catch {
return false;
}
}
I looked at that in the first place and found it didn't have an update method so had to role my own. I have two other methods that work ok to select lists of parents and get individual children but this update one baffles me. I think the db.xxx methods are an Umbraco construct which is why I haven't asked stackoverflow. Debugging stops on the db.Update line.
Not really, I don't want to create a new relation, I just want to update an existing one. I just need a bit of help with the syntax. My query does exactly what I want it to do, I just can't fire it in situ.
I am not 100% sure (99.9% :)), but I suggest to dig more into Update(string sql) method which you are trying to use. In my opinion, if you're writing complete SQL query it's easier to just call Execute with proper & desired behavior / return values. Update() method is constructing a query based on your sql input. Try a different veriant of Update method or just execute the query directly.
It may look like below:
db.Execute("UPDATE umbracoRelation SET parentId = @parentId WHERE childId = @childId", new { parentId = newParentId, childId = siteId });
WebApi Update Query
V7.4.3 building a Back Office "Relations Manager" plugin which I hope to release as a package. The very last step is to update the umbracoRelation table. I have the query which works when run independently. I can call the controller, but I can't quite get the syntax for the db to actually update. Any nudge in the right direction would be appreciated. Here's what I have atm which fails at
db.Update(query);
:-Thnx,
Craig
Hi Craig
You should use the relation service instead. https://our.umbraco.org/documentation/Reference/Management/Services/RelationService
Hi Søren,
I looked at that in the first place and found it didn't have an update method so had to role my own. I have two other methods that work ok to select lists of parents and get individual children but this update one baffles me. I think the db.xxx methods are an Umbraco construct which is why I haven't asked stackoverflow. Debugging stops on the db.Update line.
Relate method in RelationService is using AddOrUpdate() method from repository (https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/Services/RelationService.cs, line 393). Is this not enough to cover your scenario?
Hi Marcin,
Not really, I don't want to create a new relation, I just want to update an existing one. I just need a bit of help with the syntax. My query does exactly what I want it to do, I just can't fire it in situ.
Ok, understood.
I am not 100% sure (99.9% :)), but I suggest to dig more into Update(string sql) method which you are trying to use. In my opinion, if you're writing complete SQL query it's easier to just call Execute with proper & desired behavior / return values. Update() method is constructing a query based on your sql input. Try a different veriant of Update method or just execute the query directly.
It may look like below:
Big #H5YR!
Your code example was the syntax I'd been looking for for days.
Many thanks.
Happy to help!
is working on a reply...