-
Notifications
You must be signed in to change notification settings - Fork 63
Reading the database
To research the proper way to read/write to the database through Yii, you can start with Creating Records in their docs. There are actually three different ways and they each serve different purposes.
The primary way to access the database is through the Yii ORM (Object-Relational Mapping), which treats records as objects. You can query for a record using the ::model()
function to do this for any table. Yii provides functions for automatic parameter passing (binding), such as
Cart::model()->updateAll(
array(
'sort_order'=>null),
'category = :cat',
array(':cat'=>'shipping')
);
because Yii filters out for injection attacks, etc, instead of writing SQL directly.
The Cart is a special case, however. It is actually a Component, that is a middleware layer between you and the tables. If you want to add to, remove or manipulate the shopping cart, you must access it by assigning it to a variable like this:
$objCart = Yii::app()->shoppingcart;
And that's it. Now you have the cart and you can use functions found in protected/components/ShoppingCart.php such as addProduct()
, clearCart()
, updateItemQuantity()
, etc. This is what you should do instead of accessing the Cart:: model()
directly, since that may change. the ShoppingCart component is the standard object that will remain available for you.