php - CakePHP save objects state -
i'm trying out cakephp framework, , have question objects state.
previously, have been used serializing objects , unserializing them upon reload. in way, objects keep state, i'm not sure if that's best practice.
what best practice keep track of products added shopping cart , general state of model? save object state somehow in sessions? or keep data in sessions , rebuild model when page reloaded?
does cakephp offer build-in functionality should know when comes objects , states?
it seemed fitting answer questions in reverse order.
does cakephp offer build-in functionality should know when comes objects , states?
yes!, cakephp has built in wrapper php $_session
object can add , remove objects , supplied convenience methods.
what best practice keep track of products added shoopingcart , general state of model? save object state somehow in session? or keep data in session , rebuild model if page reloaded?
i never found need persist instances of php classes in applications because framework objects (e.g. controllers, models, etc.) stateless. since records database stored in associative arrays, there no problems serializing them.
the best way think framework thing have state across page reloads database... , session if choose use it.
in case following (in controller):
public function addproducttocart($productid){ // find product in database (model) $product => $this->product->findbyid($productid); // existing state of basket $basket = $this->session->read('basket'); // in case basket hasn't been initialised if($basket == null){ $basket = array(); } // append basket database $basket[] = $product; // write basket session $this->session->write('basket', $basket); }
please read documentation show how use session object in view well.
Comments
Post a Comment