stodge Posted July 23, 2008 Share Posted July 23, 2008 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 Link to comment https://forums.phpfreaks.com/topic/116193-solved-how-to-write-propel-like-oo-abstraction-programatically/ Share on other sites More sharing options...
stodge Posted July 23, 2008 Author Share Posted July 23, 2008 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 Link to comment https://forums.phpfreaks.com/topic/116193-solved-how-to-write-propel-like-oo-abstraction-programatically/#findComment-597513 Share on other sites More sharing options...
stodge Posted July 25, 2008 Author Share Posted July 25, 2008 I found this, which really helps. I didn't realise PHP had __call, __set and __get, like Python. Cool! I need to adapt it for joining two classes, but it's a good start. Link to comment https://forums.phpfreaks.com/topic/116193-solved-how-to-write-propel-like-oo-abstraction-programatically/#findComment-599454 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.