LiteCommerce:Database layer

From X-Cart 4 Classic
Jump to: navigation, search

All database operations in LiteCommerce come through the special base class Base. If you wish to create an object that stores and retrieves its properties from the database, create an object class and inherit it from the Base class.

Example 4.1 Base derived class.

// a simple class represents Person
class Person extends Base
  var $fields = array(                     // fileds array describes Person properties
  “person_id” => 0,           // These fields available through get($name) and set($name)
  “name” => “”,                 // You can set default values for properties here
  “age” => 0

  var $alias = “person”;                  // The SQL database table alias for object
  // LiteCommerce will look for “xlite_$alias” table
  var $autoIncrement = “person_id”;  // Auto-increment field name. Used as a primary key.

// create person object
$person =& func_new(“Person”)
$person->set(“name”, “John”);
$person->set(“age”, 21);
$person_id = $person->get(“person_id”);

print “John’s ID is $person_id”;

After you create Person object and save it to the database, you can retrieve person data by specifying a unique auto-increment identifier. It’s created automatically by the database when you save object data.

Example 4.2 Retrieve object data from the database

// use $person_id from the previous example

$john =& func_new(“Person”, $person_id); // create an object associated with the ID
print “John is “ . $john->get(“age”) . “ years old”;

// the second example – use “find” method if you’re unsure that database record with ID is exists
$person =& func_new(“Person”);
if ($person->find(“person_id=”.$person_id)) {
  print $person->get(“name”) . “ is “ . $person->get(“age”) . “ years old”;
} else {
  print “Person with ID = “.$person_id.” doesn’t exist!”;  // not found..

Base class also provides methods for manipulating the whole object properties at once which are called setProperties($data) and getProperties() (also available in form set(“properties”, $data) and get(“properties”)) With these methods you can fill object properties with one call. It is useful when you create an object from html form POST data as is shown in the example below.

Example 4.3 Creating object from html form data

<form action=”admin.php” method=”POST”>
<!- NOTE: hidden fields with target and action values required here to specify the post handler à
Name <input type=”text” value=”{name:r}><br>
Age : <input type=”text” value=”{age}”><br>
<input type=”submit”>
// store person data to database
$person =& func_new(“Person”);
$person->set(“properties”, $_POST); // use POST’ed form data

For manipulating collections of objects, there are methods called findAll($conditions) and readAll(). The first method is more flexible because it allows you to specify SQL conditions for queuing database when building an object collection. Both methods return an array of objects created from the data received as a search result.

Example 4.4 Creating collection of objects

$product =& func_new(“Product”);

// create collection with instances of all products fund in database
$allProducts = $product->readAll();

// using WHERE condition to make specific query
$books = $product->findAll(“sku LIKE ‘%BOOK%’”);

After you create a Base derived object, you can update its properties and delete the object by using update() and delete() methods. To delete the whole collection of objects, use findAll() or readAll() and than delete individual instances with delete().