tags:

views:

372

answers:

1

I have a database with a table named shoppingcarts. this table has the following 3 columns:

id, sessionid, date

I have a function (AddProductToCart) in my controller (ShoppingCart). Inside my function, I have this call:

$obj = ORM::factory('shoppingcart')->where('sessionID',session_id())->find();

Now, this statement runs against the shoppingcarts table and returns a row which has the sessionid matching the current session id (PHP's session_id()). But sometimes, the sessionid does not exist in the table.

So, how do I check the value returned by that statement to make sure that a value was returned or not?

I'm confused.

+3  A: 

You are looking for the property 'loaded'. Docs

It would look something like this:

$obj = ORM::factory('shoppingcart')->where('sessionID',session_id())->find();

if($obj->loaded == TRUE)
{
  // user has a shopping cart
}
else
{
  // no shopping cart found
}
Ambirex
Ambirex,thanks for the answer. I had been looking for this property for so long. i was even tempted by the idea of iterate through all rows in the table and comparing them with the session_id() returned value, and then setting a boolean flag to true if the session_id() existed already.You saved my day !
Attilah