New Sado Version 1.1 released! Easily add ORM to your applications using our Sado Library

Sado Quick Start

This quick start guide is will help you install, setup and start using Sado.

Installation

  1. Requirements: A Web server running PHP 5.1.x or greater, and a MySQL database
  2. Download the latest version of Sado
  3. Copy the downloaded Sado library files and directories into your project directory


Bootstrap File Setup

First, let's setup the Sado bootstrap file:
  1. Open the "lib/Sado/sado.bootstrap.php" file. Under "Environment settings" you can modify the "dir_sado_lib" variable with the new path to the Sado library:$dir_sado_lib = '/var/www/myproject/lib/Sado/';Or you can leave the path as an auto path:$dir_sado_lib = dirname(__FILE__) . '/';
    In the same file, under "Autoloading" you can setup autoloading so that you don't have to include each library file required by Sado. To turn on autoloading make sure this line exists: SadoFactory::registerAutoload($dir_sado_lib);If you want to turn of autoloading because you have another autoloader present, simply comment out the line.

    Further down in the file you will see "Register Sado Instances". In this section, you will want to add your database info so Sado can connect to your database: /**
    * ==============================================================================
    * Register Sado instances
    * ==============================================================================
    * Register instance 1
    */
    SadoFactory::registerInstance(SadoFactory::DRIVER_MYSQLI)
          ->host('localhost') // add location of DB, this can be localhost or IP address
          ->username('app') // DB username
          ->password('pass00') // DB password
          ->database('test') // DB name
          ->name('First DB Connection') // title used for DB (useful for logging/debugging)
          ->schemaFile($dir_sado_lib . 'sado.schema.php'); // location of Sado schema file
  2. Save and close the "sado.bootstrap.php" file.


Schema File Setup

Next, we need to setup the database schema file. This file will tell Sado what tables and fields to use/cache in the database:
  1. First, open the file "lib/Sado/sado.schema.php"
  2. In the file there is an array. Edit the ".schema" => "cache_file_path" with the location of a place where Sado can write the schema cache file: 'cache_file_path' => './cache/sado.schema.cache.php'The cache directory must be writable.
  3. Save and close the "sado.schema.php" file


Simple Database Read

Now we can do a simple database read:
  1. Create or open an "index.php" file in your project directory, and add this code to the top of the file:// include the Sado bootstrap
    require 'lib/Sado/sado.bootstrap.php';
  2. Next, add this code to get the DB connection instance and check if the DB connection is successful: // set instance
    $db = &SadoFactory::getInstance();

    // attempt to connect to database
    if( $db->ping() )
    {
          echo 'Connection successful';
    }
    else
    {
          echo 'Failed to connect';
    }
    Save the file, and load the page in a browser
  3. If the connection is simple we can do a simple read. Open the same file and replace the code with: // set instance
    $db = &SadoFactory::getInstance();

    // do simple database read
    $db->select(" SELECT * FROM tablename; ");

    // check if read went ok
    if( $db->is_error ) // error occurred
    {
          echo 'DB read error';
    }
    else // no read error
    {
          // now we can loop through records
          foreach($db->getRecords() as $v)
          {
                // print record
                echo '<pre>' . print_r($v, true) . '</pre>';
          }
    }