is my approach to using DB_DataObject sensible?
| From: | Roger Rohrbach | Date: | Mon, 26 Sep 2005 20:19:24 +0000 |
| Subject: | is my approach to using DB_DataObject sensible? | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-21109@lists.php.net to get a copy of this message | ||
Hello,
I'm using DB_DataObject for the first time. After playing around with it a bit, and given the following:
* I don't want to edit the auto-generated classes
* I want to define behaviors for my database objects
* I want to override the fetch() method, as suggested in the
documentation, to add properties that are not stored in the
underlying tables
I've decided to extend the auto-generated classes and use the new classes in my application.
As a concrete example, here's the User class for my Web app:
<?php
require_once('DB/DataObject.php');
/*
* Load the base class (DataObject_User). There's no way to do thiswithout
* instantiating, but we discard the resulting object because we'reextending
* the class.
*/
DB_DataObject::Factory('User');
class MyAppUser extends DataObject_User {
var $role;
// override fetch() to add 'role' to the object:
function fetch() {
if ($result = parent::fetch()) {
$this->role = $this->getLink('role_id');
}
return $result;
}
}
?>
'Role_id' is the foreign key of a UserRole table.
I'd like to get feedback on whether this is a sensible approach. In particular: am I correct in saying that DB_DataObject::Factory() is the only way to load the class? I can't extend the class until it's loaded. It seems a bit inelegant, but I've not seen any other way to do this.
Comments welcome.
Thanks,
Roger Rohrbach