Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Reading the database

James Pickard edited this page Feb 20, 2015 · 1 revision

Prev | Next

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.

###Prev | Next

Clone this wiki locally