dicaORM Object-relational mapping

Door Robert Cabri - 13 december 2009


spacer
dicaORM logo


DicaORM is a powerful and easy to use ORM (object-relation mapping) library. It is based on the ActiveRecord pattern. It carries both data and behavior.



DicaORM is probably faster than phpactiverecord because it doesn't parse the database scheme to define the properties. This is also a disadvantage. You still need to define your properties or so called columns for your dicaORM models. In the example below you can see how this is done.


Download



If you want to use it or experiment with it, you can download it. You can clone the latest up-to-date version from my github. I use git to keep track of changes and advise you to do the same ;).


clone git://github.com/dicabrio/dicaORM.git


If you like to contribute, just fork it and start editing! Github is free to use.


Requirements



  • PHP 5

  • PDO


TODO



Late static binding

In PHP 5.3 Late static binding is available to do some nifty stuff. Check the findAll method in the DataRecord class, you need to define the tablename. I use the Classname (__CLASS__) as a mapping to the tablename and to use the __CLASS__ constant you need to override the findAll method in your own model. This gives clutter in your domain models and is as of PHP 5.3 not necessary anymore.


Example



First off we need to create a database table where we can map the model to. For this example I use MySQL 5 and the query to create this table should look like this:


CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`other` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Now we lay down our model that will write to the database. We define columns in the model that correspond to the table in the database. To obtain the data from the models we need to create accessors. At default all attributes or columns are protected, so not accessible.


class Test extends DataRecord {

public function __construct($iID = null) {
parent::__construct(__CLASS__, $iID);
}

// define the method to define the columns to map to the db
protected function defineColumns() {
parent::addColumn('id', DataTypes::INT, false, false);
parent::addColumn('name', DataTypes::VARCHAR, 255, false);
parent::addColumn('other', DataTypes::VARCHAR, 255, false);
}

// its optional to create getters/setters
// it will come in handy when using an IDE like eclipse of netbeans
public function getName() {
return $this->getAttr('name');
}

public function setName($sName) {
$this->setAttr('name', $sName);
}

public function getOther() {
return $this->getAttr('other');
}

public function setOther($sOther) {
$this->setAttr('other', $sOther);
}

public static function findAll() {
return parent::findAll(__CLASS__, self::ALL);
}
}


When defining of the database table and the model is done we can create a little implementation that will write data into and get data from the database


include('datarecord.class.php');
include('column.class.php');
include('columnaggr.class.php');
include('datafactory.class.php');
include('datatypes.class.php');
include('querybuilder.class.php');

try {
$oDatabase = new PDO('mysql:dbname=test;host=localhost', 'root', '******');
$oData = DataFactory::getInstance();
$oData->addConnection($oDatabase, 'default');
$oData->beginTransaction();

$aAll = Test::findAll();
foreach ($aAll as $oTesting) {
$oTesting->delete();
}

echo $iRandomValue = mt_rand(1,10);

$oTest = new Test();
$oTest->setName('Robert Cabri'.$iRandomValue);
$oTest->save();

$oTest->setName($iRandomValue.'Robert Cabri');
$oTest->save();

$oTest2 = new Test($oTest->getID());

$oData->commit();
} catch (Exception $e) {
echo $e->getMessage();
$oData->rollBack();
}

I got some great inspiration from other ORM libraries.



  • Redbean

  • Doctrine

  • Ruby on Rails ActiveRecord

  • PHP activerecord


Andere artikelen

  • Webshops go to hell!
  • Custom world with Google Maps
  • dicaORM Object-relational mapping
  • Steal browser history with JavaScript
  • Initials (javascript)



Terug

spacer

@AREA071 Wanneer is eigenlijk de beste tijd om even binnen te wandelen. De Grand Opening kan ik helaas niet bijwonen. - Volg dicabrio

spacer

To affect the quality of the day, that is the highest of arts.
Henry David Thoreau

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.