Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig100 1136 posts 2523 karma points c-trib
    Jun 23, 2016 @ 20:43
    Craig100
    0

    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);:-

        // 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;
            }
        }
    

    Thnx,

    Craig

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Jun 25, 2016 @ 21:00
    Søren Kottal
    0

    Hi Craig

    You should use the relation service instead. https://our.umbraco.org/documentation/Reference/Management/Services/RelationService

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 25, 2016 @ 21:59
    Craig100
    0

    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.

  • Marcin Zajkowski 112 posts 585 karma points MVP 7x c-trib
    Jun 25, 2016 @ 22:13
    Marcin Zajkowski
    0

    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?

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 25, 2016 @ 22:23
    Craig100
    0

    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.

  • Marcin Zajkowski 112 posts 585 karma points MVP 7x c-trib
    Jun 25, 2016 @ 23:01
    Marcin Zajkowski
    102

    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:

    db.Execute("UPDATE umbracoRelation SET parentId = @parentId WHERE childId = @childId", new { parentId = newParentId, childId = siteId });
    
  • Craig100 1136 posts 2523 karma points c-trib
    Jun 25, 2016 @ 23:33
    Craig100
    0

    Big #H5YR!

    Your code example was the syntax I'd been looking for for days.

    Many thanks.

  • Marcin Zajkowski 112 posts 585 karma points MVP 7x c-trib
    Jun 26, 2016 @ 09:45
    Marcin Zajkowski
    0

    Happy to help!

Please Sign in or register to post replies

Write your reply to:

Draft