At first glance, you're not operating on the merged object. Merge takes an
instance as an argument and returns a *new* copy of that instance that is
attached.

Model.merge(member) is essentially a NOOP in your code because you're not
using the returned, attached instance. In other words, your addOrUpdate
method is operating on a detached object in all code paths.

Derek



On Mon, Mar 16, 2009 at 2:47 PM, Charles F. Munat <[email protected]> wrote:

>
> I am trying to permit upload of a profile photo to go with a member. My
> edit method is below. (I'm using id numbers in the URL, which explains
> the first part of the code. Sue me.)
>
> The problem is that no matter where I move the damn Model.merge() call,
> I get one of two outcomes on persisting a new object, neither of them
> salutary:
>
> 1. I end up with an image file named image_0.jpg. This despite calling
> merge *before* I determine the file name. So why is the ID still 0 after
> the merge?
>
> 2. I end up with two (2) new members!
>
> It works fine on edit, so currently my workaround is to create a new
> member, then go back and upload the photo. But surely there is a way
> around this... I just can't seem to wrap my head around this detached
> stuff.
>
> def edit (xhtml : NodeSeq) : NodeSeq = {
>   val member = S.param("id").openOr("new") match {
>     case "new" => new Member()
>     case id => Model.createNamedQuery[Member]("findMemberById",
>         "id" -> getId(id)).findOne match {
>       case Full(m) => m
>       case _ => S.error("Cannot find member with id #" + id)
>         redirectTo("/admin/members/")
>     }
>   }
>
>   def addOrUpdate () = {
>
>     val f = for (
>       ul <- theUpload.is;
>       uf <- Box.legacyNullTest(ul)
>     ) yield uf
>
>     if (member.nameLast.length == 0) {
>       S.error("The member's last name cannot be blank")
>     } else {
>       Model.merge(member)
>       Model.flush()
>       f match {
>         case Full(p) => if (p.mimeType.contains("image/")) {
>           member.fileSize = p.file.length
>           member.mimeType = p.mimeType
>           member.fileName = "image_" +
>             member.id.toString + "." +
>             member.mimeType.replaceAll("image/","").
>               replaceAll("jpeg", "jpg")
>           Model.merge(member)
>          Model.flush()
>           val fos: FileOutputStream = new FileOutputStream(
>             absolutePath() + "images/profile/" +
>             member.fileName
>           )
>           fos.write(p.file)
>           fos.close()
>         }
>         case _ =>
>       }
>       redirectTo("/admin/members/")
>     }
>   }
>
>   bind("member", xhtml,
>        "name" -> FocusOnLoad(SHtml.text(member.name, member.name = _,
>          ("size", "24"))),
>        "fileUpload" -> SHtml.fileUpload(u => theUpload(Full(u))),
>        "submit" -> SHtml.submit("Save", addOrUpdate)
>   )
> }
>
> Any and all ideas appreciated.
>
> Chas.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to