Skip to content

Instantly share code, notes, and snippets.

@jmas
Last active August 29, 2015 14:04
Show Gist options
  • Save jmas/ff15a5a0444b3f0caec3 to your computer and use it in GitHub Desktop.
Save jmas/ff15a5a0444b3f0caec3 to your computer and use it in GitHub Desktop.

Storage

This is simple library for easy getting / setting data from MySQL / SQLite databases.

Methods

  • __construct(PDO $connection, $scheme=[])

Methods for working with scheme:

  • setCollectionScheme($collectionName, $scheme)
  • setScheme($scheme)
  • getScheme()

Methods for configure select:

  • __get($collectionName)
  • collection($collectionName)
  • limit($limit, $skip)
  • sort($conditions=[])
  • filter($conditions=[])
  • populate($fields=[])

Methods for select data:

  • all($fields=[])
  • one($fields=[])
  • first($fields=[])
  • last($fields=[])
  • find(...)

Methods for changing data:

  • save($data=[], $returnRecord)
  • batchSave()
  • remove($ids)

collection($name) or __get($name)

  • param $name Name of collection
  • return Storage Link to current object

Set inner cursor to collection with $name.

Example: $storage->collection('users');

sort($conditions=[])

  • param $conditions=[] Conditions for sorting
  • return Storage Link to current object

Example: $storage->collection('users')->sort([ 'name'=>-1, 'id'=>1 ])->all();

$conditions is key/value array where key is field name and value is type of sorting: -1 is descending sorting, 1 is ascending sorting.

filter($conditions=[])

  • param $conditions=[] Conditions for filtering
  • return Storage Link to current object

Example: $storage->collection('users')->filter([ 'name'=>'Piter' ])->all();

$storage->collection('users')->filter([ 'name'=>['Piter', 'Mike'] ])->all();

populate(fields=[])

  • param fields=[] List of fields
  • return Storage Link to current object

$fields is list of fields that need to be linked with other tables by one to many or many to many relation.

Example: $storage->collection('users')->populate([ 'user', 'comments' ])->all();

all(fields=[])

  • param fields=[] List of fields
  • return Array List of data

$fields is list of fields that will be returned. If $fields is empty array - will be returned all fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment