Php-tick

Simple active record implementation

View the Project on GitHub localgod/php-tick

Tutorial

This section describe how to setup php-tick

Tick Setup

There is 3 simple steps to use php-tick when you have downloaded it and copied it to your site:

Bootstrap php-tick

You only need to do this once in you application.

//Connect to the database
use Localgod\Tick\Manager;
Manager::addDefaultConnectionConfig('sqlite', 'data/db.sqlite');
Manager::setModelPath('models/');

Define your model

Create a file called User.php in your model path and fill in the following:

use Localgod\Tick\Tick;
/**
 * User
 *
 * @collection users
 * @property integer(11) id          id          - unique
 * @property string(255) firstname   first_name  - null <Unknown>
 * @property string(255) lastname    last_name   - null <Unknown>
 * @property DateTime    birthday    birthday    - null
 * @property integer(1)  member      member      - <1>
 * @property string      description description
 */
class User extends Tick
{
}

Read more about defining models

Use your model

//create	    		
$user = new User();
$user->setFirstname('John');
$user->setLastname('Doe');
$user->setBirthday(new Date());
$user->setMember(2);
$user->setDescription('Some description');
$user->save();
//retrive
$user = new User();
$user = $user->get()->whereEquals('userId', $userId)->current();

echo $user->getFirstname().' '.$user->getLastname();

Getting smart

Add the following methods to your user class:

/**
 * Get user by id
 *
 * @param integer $userId User id
 *
 * @return User
 */
public static function getById($userId)
{
	$user = new self();
	$result = $user->get()->->whereEquals('userId', $userId);
	if (!$result->isEmpty()) {
		return $result->current();
	}
	throw new RuntimeException('Invalid id:('.$userId.')');
}
/**
 * Create a user
 *
 * @param string  $firstname Firstname
 * @param string  $lastname  Lastname
 * @param integer $member    Member number
 *
 * @return void
 */
public static function create($firstname, $lastname, $member) {
	$user = new self();
	$user->setFirstname($firstname);
	$user->setLastname($lastname);
	$user->setMember($member);
	$user->save();
}

Now we can work much faster with our model:

//create
User::create('John', 'Doe', 2);

//retrive
$user = User::getById(1);

echo $user->getFirstname().' '.$user->getLastname();