myActiveRecord

:::: New version released: Version 1.0 is now ready for download at:
Download here: Wattz.net Download Page

After working on a framework for a bit, I needed some Mysql support. I googled for a standalone ActiveRecord for PHP/MySQL and all I found was a blog were some dude talked about it but never released code.

So after much thought, I decided to write my own ActiveRecord class for use with PHP and MySQL. Its exclusively for MySQL because thats the primary database I use.

I have also decided to release it on my site for all the people looking for something simple to making queries a little bit more bearable.

Also this is a beta release, and as of right now, its lacking some documentation and could use a tiny bit more flexibility. Im working on it, as this is a major part of my LAMP framework.

Requirements:
- PHP 5+ (configured with –mysql)
- MySQL 5+

UPDATE:
New version released, fixes include some escaping for helping minimize injection attacks and some added a few functions to the dynamic function list. Also changed the dynamic function syntax, instead of this_dyn_function, we are now using camel case: thisDynFunction.
New functions added: (Field is used in place of column name)

  • getFieldWhere(“example_id=1″);
  • findLikeField(‘%field’);

Also added a Query function to allow for you to write your own queries and not use dynamic functions or other built ins.
Download here: Wattz.net Download Page

Brief Description of Usage:
(MAKE SURE YOU OPEN UP ActiveRecord.class.php and CHANGE the database configuration stuff)

Please make sure you read the comments. Also in this beta version there is only one naming convention forced as far as setting up your db, the primary key for a table must be id. (or you can alter the save() function and others to use something other then id. This is mainly for save().

Also please pay attention to how the dynamic user functions are used.

<?php
require(“myActiveRecord.php”);

$active = new ActiveRecord(‘table’); //create a new ActiveRecord object.

//Set the values for the fields in your table
//$active->NAME_OF_TABLE_COLUMN or field

$active ->username = “wattz”;
$active->password=”mypassword”;
$active->email=”fake@email.com”;

//save() is used for a new record as well as updating an existing record.
//to update a record you must specify an id.

$active->save();

//the save will set $active->id to the id of the last inserted record.

echo ‘<pre>’ . print_r($active->findById($active->id), true) . ‘</pre>’;
/***
* A brief explination of dynamic user functions:
* usage of the find functions are as followed: findByFieldname
* Where fieldname is the name of a column in your table.
*
* usage of of the the delete functions are as followed: deleteByFieldname
* Where fieldname is the name of a column in your table.
**/

echo ‘<pre>’ . print_r($active->findAllWhere(“id=” . $active->id), true) . ‘</pre>’;
echo ‘<pre>’ . print_r($active->findAll(), true) . ‘</pre>’;

$active->delete_by_id($active->id);

?>

Thanks,
_Wess

One response to “myActiveRecord

  1. Do we need to use our original email or fake one? Why did you used the “fake@email.com”. Isn’t it used at anywhere?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s