Db_ActiveRecord
Db_ActiveRecord class is a base class for many other LemonStand classes, including Shop_Customer, Shop_Order, Shop_Product and others. The class has a number of methods which enable you to find and update records in the database.
Class fields
- $id - record identifier. Numeric, auto-incremental value.
Class methods
- find($id) – finds a record by its identifier.
- find_by_[any column name] - finds a record by a value of any table column. For example, you can load a specific country by its code, using the following code:
$usa = Shop_Country::create()->find_by_code('US'); - find_all() - finds all records in the database and returns the Db_DataCollection object, containing a list of found records.
- where($query) – allows to limit the result of the find() and find_all() methods my adding a SQL filter. The following example code returns all orders of a customer with Id = 4. The where method allows to use parameters in the query. That is a preferable way to pass parameters to the query, because parameter values are processed before sending to the database, in order to prevent malicious data corruption.
$orders = $order->where('customer_id=4')->find_all();$orders = $order->where('customer_id=?', $customer_id)->find_all(); - save($data = null) – saves a record to the database. The optional $data array can be used to set the record field values. Example: Another way to set record field values is the direct access:
$customer = Shop_Customer::create()->find(23); $customer->save(array('first_name'=>'john'));$customer = Shop_Customer::create()->find(23); $customer->first_name = 'john'; $customer->save();
- paginate($page_index, $records_per_page) - allows to limit the result of the find_all method with a single page of record. The parameters are the zero-based page index and a number of records to return on a single page, correspondingly. Call this method before the find_all method call. The method returns an instance of the Phpr_Pagination class, which you can use to display links to pages.
- requestRowCount() - returns a number of rows which would be returned if you call the find_all method. This method is efficient in terms of memory usage and SQL queries.
- delete() - deletes the record from the database.
- limit($row_number) - allows to limit the result of the find_all() method with a specified number of records. Example:
$orders = Shop_Order::create()->limit(10)->find_all();
- order($order_columns) - allows to order the result of the find_all() method by a specific table column. Example:
$products = Shop_Product::create()->limit(5)->order('shop_products.created_at desc')->find_all();
See also:
Next: Db_DataCollection
Previous: LemonStand front-end JavaScript Framework
Return to Reference

