« Return to Thread: [need advices] An idea to build a KISS ORM system upon Zend_Db_Table

[need advices] An idea to build a KISS ORM system upon Zend_Db_Table

by Fabien MARTY :: Rate this Message:

Reply to Author | View in Thread

Hi,

I really like ZF goals but I would like a little ORM system (not a
complete and complex thing like Propel but maybe a sort of ORM
Helper).

IMHO, Zend_Db_Table is a step in the good direction.

But there is a blocking thing with Zend_Db_Table : when you call the
"find()" method on a child class of Zend_Db_Table (like in manual),
you get a Zend_Db_Table_Row object which is really easy to manipulate
but which is impossible to extend.

To get an extensible and flexible behaviour, I propose a little
modification (a few lines only) in Zend_Db_Table (Table.php and
Rowset.php files).

- First, we need to introduce two protected properties in Table.php file

protected $_rowClassName = 'Zend_Db_Table_Row';
protected $_rowsetClassName = 'Zend_Db_Table_Rowset';

=> with default values, we won't break the API

- Then, we replace all hardcoded "new Zend_Db_Table_Row(" and "new
Zend_Db_Table_Rowset(" by "$class = $this->_rowClassName ; [...] new
$class(" and "$class = $this->_rowsetClassName ; [...] new $class(".

=> This patch won't break anything in the current behaviour of Zend_Db_Table

But...

With this patch, we can get a really more flexible behaviour (like
ActiveRecord pattern).

For example :

- we have a "user" table with following fields :
=> id (int) PK
=> firstName (string)
=> familyName (string)

- we write following classes :

class UserFactory extends Zend_Db_Table {
    protected $_name = 'user';
    protected $_rowClassName = 'User';
    protected $_rowsetClassName = 'UserSet';
}

class UserSet extends Zend_Db_Table_Rowset {}
class User extends Zend_Db_Table_Row {

    // we can add some logic here !!!
    public function completeName() {
        return $this->firstName . ' ' . $this->familyName;
    }

}

Then, we can use (for instance) :

$userFactory = new UserFactory();
$user = $userFactory->find(1);
echo($user->firstName); // same behaviour than with Zend_Db_Table_Row
echo($user->familyName); // [...]
echo($user->completeName()); // we can use our domain logic here !!!
[...]

Before proposing the patch and the idea on JIRA, I would like to get
some advices or ideas about this point.

If the patch is accepted, there are some possibilities to extend
Zend_Table to complement this KISS ORM with relations (oneToOne,
oneToMany, ManyToMany...) between classes (and tables). But maybe,
it's another story.

Regards,

--
Fabien MARTY
fabien.marty@...

 « Return to Thread: [need advices] An idea to build a KISS ORM system upon Zend_Db_Table