Code Monkey home page Code Monkey logo

activeredis's Introduction

PHP ActiveRedis

ActiveRedis is a PHP 5.3+ library that brings relational model abstraction to Redis.

  • Simple
  • Lightweight
  • Extendable

ActiveRedis synthesizes a new, unorthodox implementation of the Active record pattern, borrowing good features from other libraries, but incorporating the simplicity of Redis.

License

ActiveRedis is free to use under the MIT License.

Installation

  1. Clone activeredis into your project, or add it as a submodule.
  2. Add the following code to run once in your project:
<?php

// Initialize
include 'activeredis/ActiveRedis.php';

// Connect to Redis
ActiveRedis\Database::connect('127.0.0.1:6379');

Usage

Creating New Model Classes

The model classes can be quite simple.

<?php class Human extends ActiveRedis\Model {} ?>

CRUD: Create, Read, Update, Delete

<?php

// Create
$human = Human::create(array(
	'name' => 'Wes',
	'age' => 25,
));

// Retrieve
$human = Human::find(1);

// Update
$human->name = 'Wesley';
$human->save();

// Delete
$human->delete();

Indexes

Since Redis is NoSQL, you cannot use a WHERE clause to query for data. Instead, ActiveRedis provides Indexes which automatically index data in a customizable way.

<?php

class User extends ActiveRedis\Model {
	static $indexes = array('name');
}

// Once the user is created...
$user = User::create(array('name' => 'Wesley Roberts'));

// It is possible to find the user by name
$user = User::find(array('name' => 'Wesley Roberts'));

Associations

Associations are a very powerful addition to ActiveRedis that allows you to easily create meaningful relationships between your models. Below is a simple example where a User class is associated with potentially many Project objects. The format is simple, elastic, and easily configured. ActiveRedis is smart about namespaces and will usually guess the correct namespace for both your model & association classes.

<?php

class User extends ActiveRedis\Model {
	static $associations = array(
		'HasMany Projects',
	);
}

class Project extends ActiveRedis\Model {
	static $associations = array(
		array('BelongsTo User', 'name' => 'owner'),
	)
}

// Instantiate models
$user = new User(array('username' => 'test_user', 'password' => 'test'));
$project = new Project(array('name' => 'ActiveRedis'));

// Associate models
$user->projects[] = $project;
$project->owner = $user;

// The "DeepSave" behavior will auto-save associated models if necessary
// The "AutoAssociate" behavior will associate these models if necessary
$user->save();

It's easy to build your own types of associations. Most of the association classes are only about 20 lines of PHP. See for yourself in lib/Associations.php.

Behaviors

Behaviors allow you to easily inject functionality into models by attaching custom callbacks to model events. Below is a simple example that sets a custom value before a model is saved.

<?php

class CustomModel extends ActiveRedis\Model {
	static $behaviors = array('CustomBehavior');
}

class CustomBehavior extends ActiveRedis\Behavior {
		
	function attach($table) {
		$table->bind('beforeSave', 'CustomBehavior::beforeSave');
	}
	
	function beforeSave($model) {
		$model->customValue = true;
	}
}

If you create your own Model class that extends ActiveRedis\Model, it's easy to trigger your own custom events via $this->trigger('eventName', $arguments');.

activeredis's People

Contributors

harryheng avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.