Polymorphic Extendable Base Class
Moderator: General Moderators
Polymorphic Extendable Base Class
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Polymorphic Extendable Base Class
Sounds like either RowDataGateway or ActiveRecord. Both are very generate-able. My guess is that it is ActiveRecord as that is catnip for frameworks.
(#10850)
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Polymorphic Extendable Base Class
arborint wrote:My guess is that it is ActiveRecord as that is catnip for frameworks
Re: Polymorphic Extendable Base Class
I should mention that the goal is to be able to override the methods in the ghost class by coding classes that extend it.
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?
Code: Select all
class user extends system {
protected function setUserName($var) {
parent::setUserName($var);
}
}Re: Polymorphic Extendable Base Class
Last edited by Tobey on Fri Jun 12, 2009 8:04 am, edited 1 time in total.
Re: Polymorphic Extendable Base Class
Thanks Tobey, that's definitely going to be apart of it.
Re: Polymorphic Extendable Base Class
I don't get it. What do you mean? 
Re: Polymorphic Extendable Base Class
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:
I could then do:
Now, I want the passwords to be hashed, so I can simply modify the user class:
Make sense?
Code: Select all
class user extends system {
}
Code: Select all
$user = new user();
$user->setName('foo');
$user->setPassword('password');
$user->save();
Code: Select all
class user extends system {
protected function setPassword($pass) {
return parent::setPassword(md5($pass));
}
}
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Polymorphic Extendable Base Class
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
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.
-
Paul Arnold
- Forum Contributor
- Posts: 141
- Joined: Fri Jun 13, 2008 10:09 am
- Location: Newcastle Upon Tyne
Re: Polymorphic Extendable Base Class
CodeIgniter comes with an activerecord class. Have a look at that.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.
Re: Polymorphic Extendable Base Class
Yeah but for that I'd create a template method you can override on the system class, so you don't have that static callastions wrote:Make sense?Code: Select all
class user extends system { protected function setPassword($pass) { return parent::setPassword(md5($pass)); } }
Re: Polymorphic Extendable Base Class
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
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:
Features such as relations and validation are coded into the User object such as:
Methods are naturally extensible, i.e.
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).
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]);
?>
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');*/
}
Code: Select all
Class Image extends Model {
protected function _find($params) {
$result = parent::_find($params);
$this->getImageFile($result);
}
}
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).
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: Polymorphic Extendable Base Class
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]
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]