github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

kla / php-activerecord

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 89
    • 12
  • Source
  • Commits
  • Network (12)
  • Issues (2)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (4)
    • dev
    • eager_loading
    • master ✓
    • smoothie
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

ActiveRecord implementation for PHP — Read more

  cancel

http://www.phpactiverecord.org/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

show the maximum number in error message when length exceeds max 
kla (author)
Tue Feb 09 17:19:22 -0800 2010
commit  d5d07e08da2415cfe28314851151abf1a86250b8
tree    0aba2266b3429b813a9afceae859cb5ea4b44249
parent  1076389718201a4592191ccadfa989445c9c685f
php-activerecord /
name age
history
message
file .gitignore Sun May 24 17:50:22 -0700 2009 added couple files to ignore and removed an ext... [Kien La]
file ActiveRecord.php Tue Jan 26 07:35:48 -0800 2010 chmod a-x [kla]
file LICENSE Thu May 21 21:42:16 -0700 2009 bam [kla]
file README.md Thu Feb 04 07:49:15 -0800 2010 updated readme [kla]
file TODO Mon Jul 20 09:49:28 -0700 2009 updated todo [jpfuentes2]
directory examples/ Tue Jan 26 07:35:48 -0800 2010 chmod a-x [kla]
directory lib/ Tue Feb 09 17:19:22 -0800 2010 show the maximum number in error message when l... [kla]
directory test/ Tue Feb 09 17:19:22 -0800 2010 show the maximum number in error message when l... [kla]
README.md

PHP ActiveRecord

Version 0.9 beta

by Kien La and Jacques Fuentes

http://www.phpactiverecord.org/

Introduction

A brief summarization of what ActiveRecord is:

Active record is an approach to access data in a database. A database table or view is wrapped into a class, thus an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database; when an object is updated, the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.

More details can be found here.

This implementation is inspired and thus borrows heavily from Ruby on Rails' ActiveRecord. We have tried to maintain their conventions while deviating mainly because of convenience or necessity. Of course, there are some differences which will be obvious to the user if they are familiar with rails.

Minimum Requirements

PHP 5.3+

Supported Databases

  • MySQL
  • SQLite
  • (oracle and postgres very soon)

Installation

Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with:

  1. Setting the model auto_load directory.
  2. Configuring your database connections.

Example:

ActiveRecord\Config::initialize(function($cfg)
{
    $cfg->set_model_directory('/path/to/your/model_directory');
    $cfg->set_connections(array('development' => 'mysql://username:password@localhost/database_name'));
});

Alternatively (w/o the 5.3 closure):

$cfg = ActiveRecord\Config::instance();
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections(array('development' => 'mysql://username:password@localhost/database_name'));

Once you have configured these two settings you are done. ActiveRecord takes care of the rest for you. It does not require that you map your table schema to yaml/xml files. It will query the database for this information and cache it so that it does not make multiple calls to the database for a single schema.

Features

  • Finder methods
  • Dynamic finder methods
  • Writer methods
  • Relationships
  • Validations
  • Callbacks
  • Serializations (json/xml)
  • Transactions
  • Support for multiple adapters
  • Miscellaneous options such as: aliased/protected/accessible attributes

Here are some other features we hope to include in later versions: - Named scopes - More adapters - Relationship includes

Basic CRUD

Retrieve

These are your basic methods to find and retrieve records from your database. See the Finders section for more details.

$post = Post::find(1);
echo $post->title; # 'My first blog post!!'
echo $post->author_id; # 5

# also the same since it is the first record in the db
$post = Post::first();

# finding using dynamic finders
$post = Post::find_by_name('The Decider');
$post = Post::find_by_name_and_id('The Bridge Builder',100);
$post = Post::find_by_name_or_id('The Bridge Builder',100);

# finding using a conditions array
$posts = Post::find('all',array('conditions' => array('name=? or id > ?','The Bridge Builder',100)));

Create

Here we create a new post by instantiating a new object and then invoking the save() method.

$post = new Post();
$post->title = 'My first blog post!!';
$post->author_id = 5;
$post->save();
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)

Update

To update you would just need to find a record first and then change one of its attributes. It keeps an array of attributes that are "dirty" (that have been modified) and so our sql will only update the fields modified.

$post = Post::find(1);
echo $post->title; # 'My first blog post!!'
$post->title = 'Some real title';
$post->save();
# UPDATE `posts` SET title='Some real title' WHERE id=1

$post->title = 'New real title';
$post->author_id = 1;
$post->save();
# UPDATE `posts` SET title='New real title', author_id=1 WHERE id=1

Delete

Deleting a record will not destroy the object. This means that it will call sql to delete the record in your database but you can still use the object if you need to.

$post = Post::find(1);
$post->delete();
# DELETE FROM `posts` WHERE id=1
echo $post->title; # 'New real title'
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server