Welcome Guest, Not a member yet? Register   Sign In
Best way to serialize an object
#1

[eluser]The Hamburgler[/eluser]
Hi,
I'm building a store/basket system and I would appreciate some opinions on this issue.

The products have a lot of different properties that the user can customize (size, color, text ect) so I have created models for a product and a basket.

The basket model holds product instances in an array so each instance retains its properties.

I wish to store this basket object in a session so I only have to commit the data to a database when the user purchases their basket.

I've had a look at the session library but as it uses cookies its limited to 4k. As both models extend the CI Model class the serialized basket object is to large to store in a cookie.

I think I have a few options... which is best!
1) Find a way to serialize the basket object excluding the parent info.
2) Don't extent the Basket and Product from the Model.
3) Use php native server session to store the objects.

Thanks, I hope that makes sense.
#2

[eluser]parrots[/eluser]
Personally I try to keep my session info small and store things like that in the database, but if you want to store all that in the session I'd go with 1 or 3. For #1 you could write methods on the basket model, say 'save' and another called 'restore'. Save would return a string containing just the information that's custom in your own format (ex: 'red:Hi Mom:medium') and you'd store that in the session. Then use the restore method to parse that string back in and reinitialize the object. Admittedly that's not the cleanest implementation, but it would work.
#3

[eluser]ontguy[/eluser]
I put an overview of the approach I took here:
http://ellislab.com/forums/viewthread/83217/#418698

I used CI's session method, it takes care of the serialize/unserializie of the object.

I setup a library for the cart, I'll put the methods for saving to the database (I may skip the model), when I get to that point.
#4

[eluser]The Hamburgler[/eluser]
Thanks,
I think I'm gonna try number 3 as there's a lot of customizable data for each product and it seems a waste to store them in a database until i know they are going to be purchased.
#5

[eluser]skattabrain[/eluser]
[quote author="The Hamburgler" date="1214518164"]Thanks,
I think I'm gonna try number 3 as there's a lot of customizable data for each product and it seems a waste to store them in a database until i know they are going to be purchased.[/quote]

have you considered that someday you might want to try to salvage the sales? I'm sure you've been to site, added some items to the cart and then left ... then a time later you get emails from the store asking if you want to proceed with the order and "here is 10% off" at the same time? we do that a lot ... you'd be surprised how often it turns into sales.

you might also want to query the database to see what people are interested in as well. i'd say add it to the database.
#6

[eluser]Seppo[/eluser]
Have you think about using __sleep?
If you want to store all data but parent's var you can use some reflection and array functions, something like this:
Code:
function __sleep()
    {
        return (array_keys(array_diff(get_class_vars(__CLASS__), get_class_vars(get_parent_class($this)))));
    }




Theme © iAndrew 2016 - Forum software by © MyBB