Page 1 of 2

Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 12:11 am
by Benjamin
Not sure what to call this here.

The symfony framework creates extendable base classes which represent records from database tables. These classes consist of getters and setters for each data as well as crud methods.

The framework generates these classes from yaml files, writes them to disk and it works well.

I'm pondering whether it's possible to generate these classes on the fly (or create a class that serves the same purpose) based on the database schema (perhaps from a cached db scheme file, or even an auto generated PHP file).

This is theoretical at this point, and I think it can be done. Just want to hear what you guys think before I start trying to write it.

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 1:00 am
by Christopher
Sounds like either RowDataGateway or ActiveRecord. Both are very generate-able. My guess is that it is ActiveRecord as that is catnip for frameworks.

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 2:30 am
by allspiritseve
arborint wrote:My guess is that it is ActiveRecord as that is catnip for frameworks
:D

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 3:46 am
by Benjamin
I should mention that the goal is to be able to override the methods in the ghost class by coding classes that extend it.

Code: Select all

class user extends system {
    protected function setUserName($var) {
        parent::setUserName($var);
    }
}
The class system won't contain any methods itself. It would add relevant methods to itself depending on what extends it. Yes, I am that crazy. How close can I get to accomplishing this?

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 5:40 am
by Tobey
Hey!
You should take a look at Magic Functions, especially __call.
:)

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 7:01 am
by Benjamin
Tobey wrote:Hey!
You should take a look at Magic Funtions, especially __call.
:)
Thanks Tobey, that's definitely going to be apart of it.

Re: Polymorphic Extendable Base Class

Posted: Fri Jun 12, 2009 6:05 pm
by Luke
I don't get it. What do you mean? :?

Re: Polymorphic Extendable Base Class

Posted: Sun Jun 14, 2009 11:56 pm
by Benjamin
For demonstration purposes lets say I have a class called system. This class cannot be used by itself, it must be extended by another class. The methods available in the system class will be created dynamically based on the name of the class which extends it. So if I have a database table called users, and I define a class called users which extends the system class, I will have crud methods available to me. So just with the following code:

Code: Select all

 
class user extends system {
 
}
 
I could then do:

Code: Select all

 
$user = new user();
$user->setName('foo');
$user->setPassword('password');
$user->save();
 
Now, I want the passwords to be hashed, so I can simply modify the user class:

Code: Select all

 
class user extends system {
    protected function setPassword($pass) {
        return parent::setPassword(md5($pass));
    }
}
 
Make sense?

Re: Polymorphic Extendable Base Class

Posted: Mon Jun 15, 2009 1:06 am
by allspiritseve
Yep, definitely sounds like ActiveRecord. You can get the current class name with get_class($this), and then it's just a matter of constructing the query.

Re: Polymorphic Extendable Base Class

Posted: Mon Jun 15, 2009 10:05 am
by Benjamin
Ok cool. What's the best open source active record class? I would like one that supports foreign keys and can be used with a criteria class similar to symfony.

Re: Polymorphic Extendable Base Class

Posted: Mon Jun 15, 2009 10:47 am
by Paul Arnold
astions wrote:Ok cool. What's the best open source active record class? I would like one that supports foreign keys and can be used with a criteria class similar to symfony.
CodeIgniter comes with an activerecord class. Have a look at that.

Re: Polymorphic Extendable Base Class

Posted: Wed Jun 17, 2009 11:53 pm
by josh
astions wrote:

Code: Select all

 
class user extends system {
    protected function setPassword($pass) {
        return parent::setPassword(md5($pass));
    }
}
 
Make sense?
Yeah but for that I'd create a template method you can override on the system class, so you don't have that static call

Re: Polymorphic Extendable Base Class

Posted: Thu Jun 18, 2009 12:12 am
by Benjamin
That's how you call methods in a parent class from a class that has the same method name. It's not really static. AFAIK. Am I missing something?

Re: Polymorphic Extendable Base Class

Posted: Sat Jun 27, 2009 8:02 pm
by Minty
I have made something like this following the active record pattern to a degree, similar to frameworks such as CakePHP and CodeIgniter (and of course, Rails). Personally I've called it a 'Model' class it works like this:

Code: Select all

 
<?php
 
include_once 'classes/model.php';
 
Class User extends Model {
 
}
 
$model = Model::getModel('User');
$model->findAllByName($name, array('orderBy' => 'field asc'));
$model->findFirstById($id);
$model->save([array or object of data]);
 
?>
 
Features such as relations and validation are coded into the User object such as:

Code: Select all

 
<?php
 
Class User extends Model {
 
   protected $validate = array(
      'fieldName' => array([validation options]),
      'name' => array('maxlength' => 32, 'required', 'unique')
   );
   
   protected $hasMany = array(
      'posts' => array(
         'modelName' => 'Post',
         'foreignKey' => 'user_id',
      )
   );
 
   /* Above could also be simply be written as :
   protected $hasMany = array('posts');*/
 
}
 
Methods are naturally extensible, i.e.

Code: Select all

 
Class Image extends Model {
 
   protected function _find($params) {
      $result = parent::_find($params);
      $this->getImageFile($result);
   }
 
}
 
I assume this is the sort of thing you're talking about?

My class does not generate helper get/set methods as your proposition entails, however doing so would be relatively easy using the __call magic function (although it already looks rather bloated with the 'find' functionality).

Re: Polymorphic Extendable Base Class

Posted: Mon Jun 29, 2009 5:56 pm
by BornForCode
I think is more like TableGateway.

You implement in that base class, usually an abstract class general behavior and in the extended class you have all operations available. This can be tricky when you want to create cascade operations. PK from [table 1] is referred in [table 2] and the pk of [table 2] is referred in [table 3] and you decide that you want to delete something from [table 1]