My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Models  

Featured, Phase-Deploy
Updated Jun 28, 2009 by aventure...@gmail.com

phORM provides a powerful data modeler for rapid database setup.

It's all in the model

First and foremost phORM is an Object Relational Mapper. It's data modeling setup is similar to that found in Python's Django, with the obvious limitation that python is a pure object oriented language and PHP is not.

Setup

Data models can exist anywhere, by default phORM provides the path phorm/models/Model.php, but you don't have to place yours there. The only requirement is that you know where you put it and you know what you named the file so you can include it later.

At a minimum, every phORM model MUST include your PhormConfig.php and a call to get the config values. Check out the phORM config page to learn about the PhormConfig.php

include_once $_SERVER['DOCUMENT_ROOT']."/phorm/application/PhormConfig.php";
$config = PhormConfig::getConfig();

At this point the next step is to include the type of database records you will be dealing with as ALL of your model object MUST extend this class.

include_once $config->pathToLib."/records/MySQLRecord.php";

A note about Databases

Currently phORM only supports MySQL 5. Our plans are to bring support for Sqlite,PostgreSQL, MS SQL, and XML. If anyone is interested in helping let us know!

Building your Database Objects

We tried to make this as simple and straight forward as possible. The rules are:

  • Every object in your model is Database Table
  • Every object must define it's constructor
  • Within the constructor you define your table columns

Lets say we are building a simple Address book with only 1 Database table. Your model may look something like (based on the includes we showed previously):

class AddressBook extends MySQLRecord
{
    public function initWithColumns()
    {
        $this->id       = new PrimaryKey($this);
        $this->fname    = new TextField($this, 50);
        $this->lname    = new TextField($this, 50);
        $this->email    = new TextField($this, 50);
        $this->phone    = new TextField($this, 12);
    }
}

That is all there is to it, you have just created a Database table with 5 columns.

A note on database columns

Currently database columns are bound to their database type. In this case MySQL. You can view all of the columns in: phorm/database/mysql/. All phORM columns' 1st argument is required to be $this aka: the current class. This value is passed as a reference to the new class. All phORM columns'second arguments are the length of the column. A column defined as new Varchar($this, 50); would then create a column that is a VARCHAR with a length of 50 characters.

phORM provides many types of column objects including relationships such as: ForeignKey and ManyToMany. More on that on the phORM column page

Once you have defined your model

You have more model defined and your config setup, all that is left to get you up and running is tell phORM to created your database and tables.

This part of the process you can choose to run from the command line or execute through a browser.

  • Create a new page on the server that will be hosting your project.
  • Include your model and one more class /phorm/application/ApplicationMySQL.php
  • include_once $_SERVER['DOCUMENT_ROOT'].'/model/Model.php';
    include_once $config->pathToLib."/application/ApplicationMySQL.php";

Remember, your model can be in any location. Additionally your model includes your PhormConfig.php file. In the example on this page, we defined $config = PhormConfig::getConfig();

Since we included the model we will simply access $config again to obtain acccess to our config values.

Finally, we just run ApplicationMySQL::setup(); and that's it.

Putting it all together

The whole process from start to finish will look like:

Model Setup

<?php
include_once $_SERVER['DOCUMENT_ROOT']."/phorm/application/PhormConfig.php";
$config = PhormConfig::getConfig();
	
include_once $config->pathToLib."/records/MySQLRecord.php";

class AddressBook extends MySQLRecord
{
    public function initWithColumns()
    {
        $this->id       = new PrimaryKey($this);
        $this->fname    = new TextField($this, 50);
        $this->lname    = new TextField($this, 50);
        $this->email    = new TextField($this, 50);
        $this->phone    = new TextField($this, 12);
    }
}
?>

System Setup

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'/model/Model.php';
include_once $config->pathToLib."/application/ApplicationMySQL.php";
ApplicationMySQL::setup();

?>

Add some data

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'/model/Model.php';

$obj = new AddressBook();
$obj->fname = "Rapid";
$obj->lname = "Development";
$obj->email = "phorm@phormproject.com";
$obj->phone = "555-555-5555";

$obj->save();
?>

If you setup your PhormConfig.php properly, you should now have a new database with all of your columns and one record.

Powered by Google Project Hosting