Hi Andreas

I might be wrong, but why you just create the Basket and the relationship to Book and don't commit the context until the user logs in ? :

on adding item:
Basket row = _context.newObject(Basket.class);
row.addToBooks(someBook);

on successful login:
row.setUser(someUser);
_context.commitChanges();

If you need to store the selected Basket items before the user logs in, to allow the user to quit shopping and return later you can do that using isbn(s) and cookie(s).

Marcin



On 20/03/2008, at 6:55 PM, Andreas Pardeike wrote:
Hi,

I just started my second cayenne related project and found that I am doing the same "workaround" that I did on the first one. Now I wonder if there is
a better way to do it:

My apps usually have a shopping basket BASKET with a simple relation to i.e. BOOKS (via BASKET.isbn -> BOOKS.isbn). Now, I want customers to be able to add to the basket without being logged in and so my basket rows might not be committed to the database unless the user logs in. At that point my session which holds all uncommitted basket rows will load in existing basket rows from
the db and commit.

All this only works if I add the following overwrite to my super Basket class. Not a very nice way but proven and stable. Is there a less manual way to do this?

/Andreas Pardeike



public class Basket extends _Basket
{
// we need to fetch the Book relation manually otherwise transient baskets
 // will fail to resolve getBook()

 @Override
 public void setISBN(String isbn)
 {
   super.setISBN(isbn);

Books book = DataObjectUtils.objectForPK(getObjectContext(), Books.class, isbn);
   if(book != null)
     setBook(book);
 }
}



and here's how I add a basket row:

Basket row = _context.newObject(Basket.class);
row.setISBN(newISBN);
row.setLogin(_login == null ? "" : _login);
...
if(_login != null)
 _context.commitChanges();



Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to