Copied to clipboard

Flag this post as spam?

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


  • Sebastiaan Janssen 5061 posts 15523 karma points MVP admin hq
    Nov 05, 2009 @ 10:17
    Sebastiaan Janssen
    0

    .Save() in an AfterSave event handler?

    I've created an aftersave handler for my media items. This handler takes the uploaded file and sends it to Amazon S3. Once the upload to S3 is done, I want to save the URL to the remote file in my media item.

    However, once I set the property:

    media.getProperty("cdnUrl").Value = s3Url;
    media.Save();

    You see the problem here is obvious, after the .Save(), the aftersave event is being executed again, making this an infinite loop.

    Has anybody else run into this problem? What can be done to fix it?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 05, 2009 @ 10:22
    Aaron Powell
    0

    Just so you know the property setter actually does the saving (see this post: http://www.aaron-powell.com/blog/july-2009/the-great-umbraco-api-misconception.aspx for an explanation).

    This has been some-what addressed in v4.1 (but due to overall architectual limitations it's not possible to have it 100% solved).

    I've done sites where I've re-called the Save method during a Save event handler, the only way you can work around it is to have a property somewhere that you set a flag which indicates that you've already called the event handler.

  • Sebastiaan Janssen 5061 posts 15523 karma points MVP admin hq
    Nov 05, 2009 @ 10:46
    Sebastiaan Janssen
    0

    Errr.. Wait a minute, setting the property saves it? Not in my experience, I had to explicitly call the Save() method. 

    However, I should test this more thoroughly maybe. 

    So let me get this right: setting the property does NOT fire the event handler??

  • Sebastiaan Janssen 5061 posts 15523 karma points MVP admin hq
    Nov 05, 2009 @ 10:48
    Sebastiaan Janssen
    0

    Also, I can still safely call the media.XmlGenerate method without any events firing off?

  • Sebastiaan Janssen 5061 posts 15523 karma points MVP admin hq
    Nov 05, 2009 @ 13:00
    Sebastiaan Janssen
    0

    Thank you very much for the quick answer, saved me writing code that I don't need.

    Just to explain: setting the property will save it to the database, but I still did not see the url in my media item. However, after doing the XmlGenerate, it does show up.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 05, 2009 @ 13:08
    Aaron Powell
    0

    Yeah the reason that the database write happens then is when people create their own DataType it's the easiest way in which to ensure that some form of "save" happens.

    I'll admit that I'm not a huge fan of doing it this way, I'd much prefer each DataType to have an exposed Save method which is called, but that requires implementing breaking changes to the DataType concept which would be very bad for all of us.

    4.1 will have some improvements around this though :) (I'll blog about them soon)

Please Sign in or register to post replies

Write your reply to:

Draft