Jump to content

[SOLVED] How to write Propel like OO abstraction programatically?


stodge

Recommended Posts

I'm curious - I've already written a Wiki, forums and news site. But every class is handwritten and Sql statements are hard coded. I just found Propel and I love the simplicity of this:

 

include_once 'bookstore/Author.php';

$author = new Author();
$author->setFirstName("Leo");
$author->setLastName("Tolstoy");
// note: we don't save this yet

// 2) Create a Publisher (row of 'publisher' table)

include_once 'bookstore/Publisher.php';

$pub = new Publisher();
$pub->setName("Viking Press");
// note: we don't save this yet

// 3) Create a Book (row of 'book' table)

include_once 'bookstore/Book.php';

$book = new Book();
$book->setTitle("War & Peace");
$book->setIsbn("0140444173");
$book->setPublisher($pub);
$book->setAuthor($author);
$book->save(); // saves all 3 objects!

 

I know Propel uses XML that is used to generate code (presumably). But is there a way to do it programatically? Something akin to Django (Python)?

 

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

 

I haven't written any PHP since PHP4 so a lot of PHP's OO features may be new to me, though I'm familiar with OO development in Python, C++ and Java. Any thoughts?

 

Cheers

This is a start:

 

<?php

Model::$description = array();

class Model
{
public static $description;

public function describe()
{
	//print_r(self::$description) . "\n";
	foreach (self::$description as $field)
	{
		print "Field " . $field . "\n";
		foreach ($field as $token => $value)
		{
			print "\t" . $token . "=" . $value . "\n";
		}
	}
}

public function get()
{
	return self::$description;
}
};

Person::$description = array(
"age" => array("type" => "integer", "max" => 120, "min" => 0),
"name" => array("type" => "string", "maxlength" => 256)
);

class Person extends Model
{
};

$p = new Person();
$p->describe();

?>

 

Gives:

 

Field Array
        type=integer
        max=120
        min=0
Field Array
        type=string
        maxlength=256

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.