\lform

Littlefoot ORM > 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](http://en.wikipedia.org/wiki/Object-relational_mapping)

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.

Auto Load

You can query for all rows from any database table of format abc_efg (letters separated by underscore), with (new AbcEfg)->find()

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.

extends

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 Object

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.

Littlefoot ORM: SQLQuery

unless you define it otherwise, new will be hijacked

$blogThreads = (new BlogThreads)
    ->byId(12)
    ->find();

class mynewclass extends BlogComments { }

blogComments = (new mynewclass)->find();

pre($blogComments);

Potential Names

SQLQuery qquery qq SQLQS

Summary

Methods
Properties
Constants
__construct()
__destruct()
getIterator()
initDb()
free()
getLastResult()
is_table()
affected()
import()
dump()
fetch()
fetchAll()
escape()
query()
__get()
debug()
__toString()
q()
__callStatic()
distinct()
__call()
setPk()
find()
qFromResult()
withFk()
cols()
resultCount()
rowCount()
where()
order()
limit()
setArray()
setAsNow()
add()
saveAll()
save()
debugSQL()
first()
last()
delete()
get()
getAll()
get1byid()
rm1byid()
update1byid()
add1()
insertArray()
updateById()
rawResult()
numrows()
$debug
$data
$joins
$conditions
$where
$order
$limit
$result
$row
$error
$mysqli
$pkIndex
No constants found
No protected methods found
$table
N/A
throwException()
queryMagic()
findMagic()
joinOn()
filterBy()
set()
insert()
select()
update()
currentRow()
nextRow()
$sql
$db
$crud
$distinctCol
$mysqli_result
N/A

Properties

$debug

$debug : boolean

Type

boolean — Prints resulting $sql after execution.

$data

$data : array

Type

array — Array of data ($col => $val). Used in CRUD operations.

$joins

$joins : array

Type

array — Array of join operations.

$conditions

$conditions : array

Type

array — Array of conditions ('var > val', 'var2 = val2'). Used in where clause.

$where

$where : string

Type

string — Where clause override.

$order

$order : string

Type

string — Literal string after "ORDER BY". Usage: "id" (sort by id); "position DESC" (sort by position descending)

$limit

$limit : string

Type

string — Limit clause. Usage: "1" or "1, 3"

$result

$result : \lf\orm

Type

\lf\orm — on find(), save array() result

$row

$row : integer

Type

integer — counter for incremental ->get()

$error

$error : integer

Type

integer — counter for incremental ->get()

$mysqli

$mysqli : \lf\mysqli_result

Type

\lf\mysqli_result — the last mysqli_result. Need to move this to `$_SESSION['mysqli_result'] = array();`

$pkIndex

$pkIndex : 

$var string $pkIndex default column to update on.

Type

$table

$table : string

Type

string — Stores the table specified at orm::q('my_table')

$sql

$sql : string

Type

string — Variable used to construct the SQL query at execution

$db

$db : \lf\Database

Type

\lf\Database — Database wrapper object. $this->db

$crud

$crud : string

Type

string — Chosen CRUD operation (select, insert, update, delete)

$distinctCol

$distinctCol : string

Type

string — Add DISTINCT limitation to SQL query

$mysqli_result

$mysqli_result : \lf\mysqli_result

Type

\lf\mysqli_result — the last mysqli_result. Need to move this to `$_SESSION['mysqli_result'] = array();`

Methods

__construct()

__construct(  $table = '', \lf\Database  $db = NULL) 

Initialize `$this->mysqli_result` with `initDb()`. Pulls from $_SESSION if a previous connection already exists.

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 quer
  • Store the Database wrapper and the specified table which is ideally called from orm::q('my_table')

Parameters

$table
\lf\Database $db

Database wrapper

__destruct()

__destruct() 

Close MySQLI connection object

getIterator()

getIterator() 

So you can loop through an object collection

$pages = (new LfPages)->find();
$count=0;
foreach($pages as $page)
{
    $page->settitle('New '.$count++)->debug()->save();
}

initDb()

initDb() 

Given a database configuration, the object is instantiated. If there is an error, it is accessible at $this->error. Configuration is saved to $this->conf

free()

free() 

Free last database result

getLastResult()

getLastResult() 

Returns the last query result good for debugging

is_table()

is_table(string  $table) 

Queries the information schema for a table called $table in this database

Parameters

string $table

Check to see if $table exists.

affected()

affected() 

{ return $this->mysqli->affected_rows; }

import()

import(string  $file) 

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

Parameters

string $file

Path to .sql backup file to be imported into the configured database.

dump()

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

Dumps database or table to file.

Parameters

string $table

empty by default. If specified, only that table will be dumped from the database

string $folder

Defaults to LF.'lf/backup/'.

fetch()

fetch(  $query = NULL) 

Absorbed db class functions

Parameters

$query

fetchAll()

fetchAll(  $query = NULL) 

Parameters

$query

escape()

escape(  $str) 

$str is usually user-supplied supplied data. Don't forget to sanatize input!

Parameters

$str

query()

query(string  $q, boolean  $big = false) 

Run query, return SQL result, increment SQL counter

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

Parameters

string $q

MySQL Query

boolean $big

If the request is big

__get()

__get(  $name) 

Parameters

$name

debug()

debug() 

__toString()

__toString() 

q()

q(string  $table) : \lf\orm

Called statically (ie "orm::q()")

Parameters

string $table

Specifies the table to run queries on

Returns

\lf\orm

object

__callStatic()

__callStatic(  $method,   $args) 

Parameters

$method
$args

distinct()

distinct(  $column) 

Parameters

$column

__call()

__call(  $method,   $args) 

Parameters

$method
$args

setPk()

setPk(  $column) 

Parameters

$column

find()

find(  $args = null) 

Parameters

$args

qFromResult()

qFromResult() 

push current result row into data value

ideal to run after query to ->save()

withFk()

withFk(  $foriegn_key) 

Return a string <table>.<column> to define the key on which to join

Example

echo (new BlogComments)->withFk('parent_id');

Would print the string 'blog_comments.parent_id';

Parameters

$foriegn_key

cols()

cols(  $cols) 

Parameters

$cols

resultCount()

resultCount() 

rowCount()

rowCount() 

where()

where(  $clause) 

Parameters

$clause

order()

order(  $column = 'id',   $sort = 'ASC') 

Parameters

$column
$sort

limit()

limit(  $limit) 

Parameters

$limit

setArray()

setArray(  $set) 

Parameters

$set

setAsNow()

setAsNow(  $column) 

Parameters

$column

add()

add() 

Set object CRUD action as 'INSERT into insert statement

saveAll()

saveAll() 

save()

save() 

debugSQL()

debugSQL() 

first()

first() 

last()

last() 

delete()

delete() 

get()

get(  $row = null) 

Parameters

$row

getAll()

getAll() 

get1byid()

get1byid(  $id) 

Parameters

$id

rm1byid()

rm1byid(  $id) 

Parameters

$id

update1byid()

update1byid(  $id,   $data) 

Parameters

$id
$data

add1()

add1(  $data) 

Parameters

$data

insertArray()

insertArray(  $data) 

Parameters

$data

updateById()

updateById(  $id,   $data) 

Parameters

$id
$data

rawResult()

rawResult() 

numrows()

numrows(boolean  $make_conversion_easier = true) 

Prints number of rows in the MySQL result

Parameters

boolean $make_conversion_easier

Don't think this does anything.

throwException()

throwException(  $msg = '') 

Parameters

$msg

queryMagic()

queryMagic(  $table,   $args) 

# BROKEN

I broke this somehow and rather than fix it, I rely on the __autoload method (ie, new LfActions).

Accessible like qPages('lf') or queryUsers('lf')

Parameters

$table
$args

findMagic()

findMagic(  $columns,   $args) 

Parameters

$columns
$args

joinOn()

joinOn(string  $foreignKey, string  $args) 

## Example

$blogPost = (new BlogThreads)
->joinOnId( (new BlogComments)->withFk('parent_id') )
->findById(12);

Parameters

string $foreignKey

The local column to use with a join

string $args

$args[0] is the table.column string (ideally generated with $this->withFk).

filterBy()

filterBy(  $column,   $args) 

Parameters

$column
$args

set()

set(  $column,   $args) 

Parameters

$column
$args

insert()

insert() 

select()

select() 

update()

update() 

currentRow()

currentRow() 

nextRow()

nextRow()