PHP Entity Generator (896 words)

A while ago I was using the Django Framework and was a big fan of the parts of it which save time. One part which I both loved and hated was the ORM. The bit I loved was for creating new database entities, loading them and modifying them. The ability to just load up an object and modify it and then call its save method saved me a lot of time. What I hated about it however was using it for doing any form of complex query (since I am very comfortable with SQL) and working backwards by designing the model then the database.

Get the source at PHP Entity Generator on Google Code

A while ago I needed to do some coding in PHP and missed the ORM part of Django so much I created a simple script which would generate entities based on tables in the database. In order to make things a bit simpler I added the following requirements,

  1. Would only support the following types, int, varchar, text, datetime
  2. Would generate entities off the database and not the reverse
  3. Would require a autoincrement id used as the primary key
  4. Would try to be as typesafe as possible
  5. Would generate correct inject safe SQL
  6. Would generate code that could be extended if say I needed a method to get by some other type
  7. Required that a PDO database object be passed in for database operations

With those requirements I got to coding, and after a while I had something that outputs the following example, (please excuse the lack of formatting I have a lack of CSS/HTML skills)

<?php
//////////////////////////////////////////////////////////////
// This class generated by a tool on 2010-09-15 at 08:58:58 //
//////////////////////////////////////////////////////////////
class article {
private $_id = null;
private $_rssid = null;
private $_title = null;
private $_link = null;
private $_date = null;
private $_description = null;
private $_author = null;
private $_publisheddatetime = null;
private $_image = null;

function getid() {
return $this->_id;
}
function getrssid() {
return (int)$this->_rssid;
}
function gettitle() {
return $this->_title;
}
function getlink() {
return $this->_link;
}
function getdate() {
return $this->_date;
}
function getdescription() {
return $this->_description;
}
function getauthor() {
return $this->_author;
}
function getpublisheddatetime() {
return strtotime($this->_publisheddatetime);
}
function getimage() {
return $this->_image;
}
function setrssid($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - rssid");
}
if(gettype($newvalue) != "integer") {
throw new Exception("Value not integer - ".$newvalue);
}
if(strlen($newvalue) > 10) {
throw new Exception("Value size larger then then - 10 - ".$newvalue);
}
$this->_rssid = (int)$newvalue;
}
function settitle($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - title");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
if(strlen($newvalue) > 1000) {
throw new Exception("Value size larger then then - 1000 - ".$newvalue);
}
$this->_title = $newvalue;
}
function setlink($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - link");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
if(strlen($newvalue) > 500) {
throw new Exception("Value size larger then then - 500 - ".$newvalue);
}
$this->_link = $newvalue;
}
function setdate($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - date");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
if(strlen($newvalue) > 100) {
throw new Exception("Value size larger then then - 100 - ".$newvalue);
}
$this->_date = $newvalue;
}
function setdescription($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - description");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
$this->_description = $newvalue;
}
function setauthor($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - author");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
if(strlen($newvalue) > 100) {
throw new Exception("Value size larger then then - 100 - ".$newvalue);
}
$this->_author = $newvalue;
}
function setpublisheddatetime($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - publisheddatetime");
}
if(gettype($newvalue) != "integer") {
throw new Exception("Value not integer - ".$newvalue);
}
$this->_publisheddatetime = date("Y-m-d H:i:s",(int)$newvalue);
}
function setimage($newvalue) {
if(is_null($newvalue)) {
throw new Exception("Value cannot be null - image");
}
if(gettype($newvalue) != "string") {
throw new Exception("Value not string - ".$newvalue);
}
if(strlen($newvalue) > 500) {
throw new Exception("Value size larger then then - 500 - ".$newvalue);
}
$this->_image = $newvalue;
}

function save($db) {
if(is_null($this->_id)) {
$query = $db->prepare("INSERT INTO `article` VALUES (NULL,?,?,?,?,?,?,?,?);");
$query->execute(array($this->_rssid,$this->_title,$this->_link,$this->_date,$this->_description,$this->_author,$this->_publisheddatetime,$this->_image));
$this->_id = $db->lastInsertId();
}
else {
$query = $db->prepare("UPDATE `article` SET `rssid`=?,`title`=?,`link`=?,`date`=?,`description`=?,`author`=?,`publisheddatetime`=?,`image`=? WHERE `id`=? LIMIT 1;");
$query->execute(array($this->_rssid,$this->_title,$this->_link,$this->_date,$this->_description,$this->_author,$this->_publisheddatetime,$this->_image,$this->_id));
}
}

function delete($db) {
if(is_null($this->_id)) {
return;
}
else {
$query = $db->prepare("DELETE FROM `article` WHERE `id`=? LIMIT 1;");
$query->execute(array($this->_id));
}
}

function get($db,$id) {
if(gettype($id) != "integer") {
throw new Exception("Value not integer");
}
else {
$query = $db->prepare("SELECT `id`,`rssid`,`title`,`link`,`date`,`description`,`author`,`publisheddatetime`,`image` FROM `article` WHERE `id`=? LIMIT 1;");
$query->execute(array($id));
foreach ($query->fetchAll() as $row_id => $row_data) {
$this->_id = $row_data["id"];
$this->_rssid = $row_data["rssid"];
$this->_title = $row_data["title"];
$this->_link = $row_data["link"];
$this->_date = $row_data["date"];
$this->_description = $row_data["description"];
$this->_author = $row_data["author"];
$this->_publisheddatetime = $row_data["publisheddatetime"];
$this->_image = $row_data["image"];
}
}
}
}
?>

Not too shabby I think! I have added the source to Google Code under the new BSD licence which should make it open enough for anyone to do whatever they want with it. It is missing unit tests any form of objects or even comments but it is quite useable. You can find the PHP Entity project in the link hosted at Google Code.