Back to Littlefoot Manual

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. - Wikipedia, the free encyclopedia

Littlefoot comes with a schema-agnostic ORM class which offers an object oriented approach to database manipulation. The idea is to store different pieces of a query as variables in the object and allow you to manipulate those variables using various object methods before execution. This ORM implements PHP's magic method __call which allows arbitrary column names to be used in the method call using regex.

Table of Contents

* Construct
* Database Methods
* Set table 
 * q
 * __autoload
 * extends
* get
 * getAll
 * first
* find
 * orm::$row
 * orm::results
* columns (cols)
* orderBy$Column
* filterBy$Column
 * by$Column
 * getBy$Column
 * getAllBy$Column
 * findBy$Column
* __toString
* set$Column
 * setArray
 * insertArray
* joinOn$Column


You can initialize the orm as follows:

$myOrm = (new orm);

But you can't do much with it as the table target is fundamental to making a query.

So lets set the table.


You can use ->q($table) to set the table in the orm object.

$myTable = (new orm)->q($table);

eg, $users = (new orm)->q('lf_users');


There is a fun shortcut to the above where you can just call new class instances out of a given table name using __autoload.

$users = (new LfUsers);

If you were to call (new BlogThreads), the autoload function would quickly define a class called BlogThreads extended from the orm on the fly with a table set as blog_threads.


You can extend from orm.

class myPages extends orm { public $table = 'app_pages'; }

And for lulz, you can extend from an __autoload'd class name to define the table on the fly.

class myPages extends AppPages {}

Database methods

The first time this is called, a new mysqli object will be created and stored in $_SESSION['db']. After that, the same mysqli object is simply returned. If you are in the littlefoot context, this is already likely accessible at $this->db.

These database methods are used by the ORM to execute SQL statements. You can use them too!

sqlresult query('SELECT * FROM table')

Run query, return SQL result, increment SQL counter

$sqlResult = (new orm)->query('SELECT * FROM lf_users');

array fetch('SELECT * FROM table')

Run query and return first row returned as an array. Increment SQL counter.

$row = (new orm)->fetch('SELECT * FROM lf_users');

array fetchall('SELECT * FROM table')

Run query and return matrix array of the rows:

$rows = (new orm)->fetchall('SELECT * FROM lf_users');

  0 => array(id => 1, col1 => 'valA', col2 => 'val1', ...), 
  1 => array(id => 2, col1 => 'valB', col2 => 'val2', ...)

void import($file)

SQL commands are preg_match()'d out of $file and run in a loop with errors suppressed

preg_match_all("/(?:^|\n)([A-Z][^;]+);/", $dump, $match);

void dump($table = '', $folder = NULL)

$folder is the destination, $table will print only the table instead of the whole database

string escape($string)

$string is usually user-supplied supplied data. Don't forget to sanitize input!


get returns the query results as an array. Without any other method run before get, this essentially runs SELECT * FROM lf_actions.

$action = (new LfActions)->get();


getAll returns all rows of the query result in a 2D array

$actions = (new LfActions)->getAll();


And if you only need the first row of the result:

$firstUser = (new LfUsers)->first();


This works when there are multiple results too. ->get() returns the current row data array and increments the internal $this->row index until the index goes beyond the index range of the results.

foreach( (new LfActions)->getAll as $action )
    echo $action['label'];


The methods of the object modify some internal variable then return $this (the object itself). That allows for a daisy chain of method calls.

orm can be used as a collection and as an individual record. orm objects have an internal $this->row variable that tracks your index in the MySQL result rows. By default we are always looking at the first row returned (index 0). In the case where only a single row is returned, this approach allows us to use the result as a single row of column data:

$action = (new LfActions)

echo $action->label;

Loop with get, but show current row column data with ->$Column.

$action = (new LfActions)->find();

    echo $action->Label.'<br />';


To limit the SELECT columns, provide them in an comma separated list of names in a string.

$pageList = (new LfPages)->cols('id, title');


Allows you to sort by a column. Like sorting alphabetically by title.

$sortedPageList = (new LfPages)
    ->cols('id, title')
    ->orderBy('title DESC');


  • Get Page with id of 5:

    $page = (new LfPages)->filterById(5)->find();


  • Same thing but with a shortcut:

    $page = (new LfPages)->byId(5)->find();


  • Same thing but with a shortercut:

    $page = (new LfPages)->findById(5);


  • Same thing but with a get rather than find:

    $page = (new LfPages)->getById(5);


  • Set title to "my new title" where id = 4
(new LfPages)
  ->setTitle("my new title")


  • Set $data from $_POST and save.

    (new LfPages)->setArray($_POST)->save()


  • Saving directly

    (new LfPages)->insertArray($_POST);


How to join:

(new BlogThreads)->byId(5)
   (new BlogComments)->withFk('parent_id')


echo (new LfPages); would print a plain text list of each row contents. It auto-runs a find() and just prints the results.

Object Iteration

Foreach will loop over result rows and generate orm objects with that row as its data

$collection = (new LfActions)->find();

foreach($collection as $i => $orm)
    echo $count++;

        $orm->setLabel('my new label')->save(); // set all child elements label
blog comments powered by Disqus