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

12 messages Options
Embed this post
Permalink
Fabien MARTY

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

Reply Threaded More More options
Print post
Permalink
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
[hidden email]
Rodrigo Moraes

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

Reply Threaded More More options
Print post
Permalink
On 7/11/06, Fabien MARTY wrote:
> - First, we need to introduce two protected properties in Table.php file
> protected $_rowClassName = 'Zend_Db_Table_Row';
> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';

I think this makes a lot of sense and adds a lot of flexibility to
Zend_Db_Table. We can make our own base class subclassing
Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
implement this idea, but I think it would be worth to have this in
Zend_Db_Table itself, unless there are other plans for these
methods...

cheers,
rodrigo moraes / brazil
Victor Bolshov

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

Reply Threaded More More options
Print post
Permalink
In reply to this post by Fabien MARTY
Fabien MARTY wrote:

> 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';
>

There was already some discussion related to this. Personally, I totally
agree with you.

Regards, Victor.
sylvain bannier

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

Reply Threaded More More options
Print post
Permalink
In reply to this post by Rodrigo Moraes

Rodrigo Moraes wrote:
On 7/11/06, Fabien MARTY wrote:
> - First, we need to introduce two protected properties in Table.php file
> protected $_rowClassName = 'Zend_Db_Table_Row';
> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';

I think this makes a lot of sense and adds a lot of flexibility to
Zend_Db_Table. We can make our own base class subclassing
Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
implement this idea, but I think it would be worth to have this in
Zend_Db_Table itself, unless there are other plans for these
methods...

cheers,
rodrigo moraes / brazil
Hello,
I made a few steps in this way. if you're interested, i could send you my files.
regards,
Davey Shafik-3

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

Reply Threaded More More options
Print post
Permalink
I actually have a fully working Rails-like (not /too/ similar though)  
ActiveRecord implementation that got lost (bureaucratically)  
somewhere in the mix.

The only feature I would like to add is the possibility to resolve  
relationships automatically, such that it can condense the number of  
queries down using JOINs.

- Davey

On Sep 22, 2006, at 10:04 AM, sylvain bannier wrote:

>
>
>
> Rodrigo Moraes wrote:
>>
>> On 7/11/06, Fabien MARTY wrote:
>>> - First, we need to introduce two protected properties in  
>>> Table.php file
>>> protected $_rowClassName = 'Zend_Db_Table_Row';
>>> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
>>
>> I think this makes a lot of sense and adds a lot of flexibility to
>> Zend_Db_Table. We can make our own base class subclassing
>> Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
>> implement this idea, but I think it would be worth to have this in
>> Zend_Db_Table itself, unless there are other plans for these
>> methods...
>>
>> cheers,
>> rodrigo moraes / brazil
>>
>>
>
> Hello,
> I made a few steps in this way. if you're interested, i could send  
> you my
> files.
> regards,
> --
> View this message in context: http://www.nabble.com/-need-advices-- 
> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> tf1926214.html#a6447689
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

sylvain bannier

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

Reply Threaded More More options
Print post
Permalink
I actually have implemented a feature that enables related objects fetch based on columns name.

providing your table has a field named "fk_myRelatedTable" or something like that, you can get related objects by usings methods like this :

$relatedObject = $myParentObject->getMyRelatedObject();

This method automatically performs the "joins".





Synaptic Media wrote:
I actually have a fully working Rails-like (not /too/ similar though)  
ActiveRecord implementation that got lost (bureaucratically)  
somewhere in the mix.

The only feature I would like to add is the possibility to resolve  
relationships automatically, such that it can condense the number of  
queries down using JOINs.

- Davey

On Sep 22, 2006, at 10:04 AM, sylvain bannier wrote:

>
>
>
> Rodrigo Moraes wrote:
>>
>> On 7/11/06, Fabien MARTY wrote:
>>> - First, we need to introduce two protected properties in  
>>> Table.php file
>>> protected $_rowClassName = 'Zend_Db_Table_Row';
>>> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
>>
>> I think this makes a lot of sense and adds a lot of flexibility to
>> Zend_Db_Table. We can make our own base class subclassing
>> Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
>> implement this idea, but I think it would be worth to have this in
>> Zend_Db_Table itself, unless there are other plans for these
>> methods...
>>
>> cheers,
>> rodrigo moraes / brazil
>>
>>
>
> Hello,
> I made a few steps in this way. if you're interested, i could send  
> you my
> files.
> regards,
> --
> View this message in context: http://www.nabble.com/-need-advices-- 
> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> tf1926214.html#a6447689
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
Davey Shafik-3

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

Reply Threaded More More options
Print post
Permalink
Sylvain,

I have this, but its called on the record object, so:

$dog = $do->find();
$fleas = $dog->getAllFleas();

But if you have 300 dogs it's 1+{Numer of Rows) the queries. If  
however when you do something like:

$dogs = $do->fetchAll();

it automatically did a JOIN there, it's one query.

- Davey

On Sep 22, 2006, at 11:21 AM, sylvain bannier wrote:

>
> I actually have implemented a feature that enables related objects  
> fetch
> based on columns name.
>
> providing your table has a field named "fk_myRelatedTable" or  
> something like
> that, you can get related objects by usings methods like this :
>
> $relatedObject = $myParentObject->getMyRelatedObject();
>
> This method automatically performs the "joins".
>
>
>
>
>
>
> Synaptic Media wrote:
>>
>> I actually have a fully working Rails-like (not /too/ similar though)
>> ActiveRecord implementation that got lost (bureaucratically)
>> somewhere in the mix.
>>
>> The only feature I would like to add is the possibility to resolve
>> relationships automatically, such that it can condense the number of
>> queries down using JOINs.
>>
>> - Davey
>>
>> On Sep 22, 2006, at 10:04 AM, sylvain bannier wrote:
>>
>>>
>>>
>>>
>>> Rodrigo Moraes wrote:
>>>>
>>>> On 7/11/06, Fabien MARTY wrote:
>>>>> - First, we need to introduce two protected properties in
>>>>> Table.php file
>>>>> protected $_rowClassName = 'Zend_Db_Table_Row';
>>>>> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
>>>>
>>>> I think this makes a lot of sense and adds a lot of flexibility to
>>>> Zend_Db_Table. We can make our own base class subclassing
>>>> Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
>>>> implement this idea, but I think it would be worth to have this in
>>>> Zend_Db_Table itself, unless there are other plans for these
>>>> methods...
>>>>
>>>> cheers,
>>>> rodrigo moraes / brazil
>>>>
>>>>
>>>
>>> Hello,
>>> I made a few steps in this way. if you're interested, i could send
>>> you my
>>> files.
>>> regards,
>>> --
>>> View this message in context: http://www.nabble.com/-need-advices--
>>> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
>>> tf1926214.html#a6447689
>>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/-need-advices-- 
> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> tf1926214.html#a6449160
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

sylvain bannier

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

Reply Threaded More More options
Print post
Permalink
I've implemented the get<Related> method on rows AND "row sets" (actually a subclass of rowset). So you can get the related objects in 2 queries

$puppies = $do->findAllPuppies();
// $puppies is a row_set
$puppiesFleas = $puppies->getAllFleas();

Sylvain


Synaptic Media wrote:
Sylvain,

I have this, but its called on the record object, so:

$dog = $do->find();
$fleas = $dog->getAllFleas();

But if you have 300 dogs it's 1+{Numer of Rows) the queries. If  
however when you do something like:

$dogs = $do->fetchAll();

it automatically did a JOIN there, it's one query.

- Davey

On Sep 22, 2006, at 11:21 AM, sylvain bannier wrote:

>
> I actually have implemented a feature that enables related objects  
> fetch
> based on columns name.
>
> providing your table has a field named "fk_myRelatedTable" or  
> something like
> that, you can get related objects by usings methods like this :
>
> $relatedObject = $myParentObject->getMyRelatedObject();
>
> This method automatically performs the "joins".
>
>
>
>
>
>
> Synaptic Media wrote:
>>
>> I actually have a fully working Rails-like (not /too/ similar though)
>> ActiveRecord implementation that got lost (bureaucratically)
>> somewhere in the mix.
>>
>> The only feature I would like to add is the possibility to resolve
>> relationships automatically, such that it can condense the number of
>> queries down using JOINs.
>>
>> - Davey
>>
>> On Sep 22, 2006, at 10:04 AM, sylvain bannier wrote:
>>
>>>
>>>
>>>
>>> Rodrigo Moraes wrote:
>>>>
>>>> On 7/11/06, Fabien MARTY wrote:
>>>>> - First, we need to introduce two protected properties in
>>>>> Table.php file
>>>>> protected $_rowClassName = 'Zend_Db_Table_Row';
>>>>> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
>>>>
>>>> I think this makes a lot of sense and adds a lot of flexibility to
>>>> Zend_Db_Table. We can make our own base class subclassing
>>>> Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
>>>> implement this idea, but I think it would be worth to have this in
>>>> Zend_Db_Table itself, unless there are other plans for these
>>>> methods...
>>>>
>>>> cheers,
>>>> rodrigo moraes / brazil
>>>>
>>>>
>>>
>>> Hello,
>>> I made a few steps in this way. if you're interested, i could send
>>> you my
>>> files.
>>> regards,
>>> --
>>> View this message in context: http://www.nabble.com/-need-advices--
>>> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
>>> tf1926214.html#a6447689
>>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/-need-advices-- 
> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> tf1926214.html#a6449160
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
Peter Hodge-5

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

Reply Threaded More More options
Print post
Permalink
I had a few special requirements for *my ORM*, which ZF ORM would need to cater
for:

- I need to configure the ORM classes to automatically add 'WHERE dogs.Archived
= 0' when doing a '$dogs->fetchAll()'

- If the current user has access only to certain breeds of dogs, the ORM
classes need to automatically add 'JOIN dogs_breeds ... WHERE dogs_breeds
IN({$user->allowedBreeds()})' when doing a '$dogs->fetchAll()'.

This allowed me to deal with 'hidden' records and a detailed security model
without re-specifying the rules everywhere in my application - it saved my alot
of time and testing.

cheers,
Peter



--- sylvain bannier <[hidden email]> wrote:

>
> I've implemented the get<Related> method on rows AND "row sets" (actually a
> subclass of rowset). So you can get the related objects in 2 queries
>
> $puppies = $do->findAllPuppies();
> // $puppies is a row_set
> $puppiesFleas = $puppies->getAllFleas();
>
> Sylvain
>
>
>
> Synaptic Media wrote:
> >
> > Sylvain,
> >
> > I have this, but its called on the record object, so:
> >
> > $dog = $do->find();
> > $fleas = $dog->getAllFleas();
> >
> > But if you have 300 dogs it's 1+{Numer of Rows) the queries. If  
> > however when you do something like:
> >
> > $dogs = $do->fetchAll();
> >
> > it automatically did a JOIN there, it's one query.
> >
> > - Davey
> >
> > On Sep 22, 2006, at 11:21 AM, sylvain bannier wrote:
> >
> >>
> >> I actually have implemented a feature that enables related objects  
> >> fetch
> >> based on columns name.
> >>
> >> providing your table has a field named "fk_myRelatedTable" or  
> >> something like
> >> that, you can get related objects by usings methods like this :
> >>
> >> $relatedObject = $myParentObject->getMyRelatedObject();
> >>
> >> This method automatically performs the "joins".
> >>
> >>
> >>
> >>
> >>
> >>
> >> Synaptic Media wrote:
> >>>
> >>> I actually have a fully working Rails-like (not /too/ similar though)
> >>> ActiveRecord implementation that got lost (bureaucratically)
> >>> somewhere in the mix.
> >>>
> >>> The only feature I would like to add is the possibility to resolve
> >>> relationships automatically, such that it can condense the number of
> >>> queries down using JOINs.
> >>>
> >>> - Davey
> >>>
> >>> On Sep 22, 2006, at 10:04 AM, sylvain bannier wrote:
> >>>
> >>>>
> >>>>
> >>>>
> >>>> Rodrigo Moraes wrote:
> >>>>>
> >>>>> On 7/11/06, Fabien MARTY wrote:
> >>>>>> - First, we need to introduce two protected properties in
> >>>>>> Table.php file
> >>>>>> protected $_rowClassName = 'Zend_Db_Table_Row';
> >>>>>> protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
> >>>>>
> >>>>> I think this makes a lot of sense and adds a lot of flexibility to
> >>>>> Zend_Db_Table. We can make our own base class subclassing
> >>>>> Zend_Db_Table and changing fetchNew(), fetchRow() and fetchAll() to
> >>>>> implement this idea, but I think it would be worth to have this in
> >>>>> Zend_Db_Table itself, unless there are other plans for these
> >>>>> methods...
> >>>>>
> >>>>> cheers,
> >>>>> rodrigo moraes / brazil
> >>>>>
> >>>>>
> >>>>
> >>>> Hello,
> >>>> I made a few steps in this way. if you're interested, i could send
> >>>> you my
> >>>> files.
> >>>> regards,
> >>>> --
> >>>> View this message in context: http://www.nabble.com/-need-advices--
> >>>> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> >>>> tf1926214.html#a6447689
> >>>> Sent from the Zend Framework mailing list archive at Nabble.com.
> >>>>
> >>>
> >>>
> >>>
> >>
> >> --
> >> View this message in context: http://www.nabble.com/-need-advices-- 
> >> An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-
> >> tf1926214.html#a6449160
> >> Sent from the Zend Framework mailing list archive at Nabble.com.
> >>
> >
> >
> >
>
> --
> View this message in context:
>
http://www.nabble.com/-need-advices--An-idea-to-build-a-KISS-ORM-system-upon-Zend_Db_Table-tf1926214.html#a6452241
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>



       

       
               
____________________________________________________
On Yahoo!7
Messenger - IM with Windows Live™ Messenger friends.
http://au.messenger.yahoo.com 

Victor Bolshov

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

Reply Threaded More More options
Print post
Permalink
Peter Hodge wrote:

> I had a few special requirements for *my ORM*, which ZF ORM would need to cater
> for:
>
> - I need to configure the ORM classes to automatically add 'WHERE dogs.Archived
> = 0' when doing a '$dogs->fetchAll()'
>
> - If the current user has access only to certain breeds of dogs, the ORM
> classes need to automatically add 'JOIN dogs_breeds ... WHERE dogs_breeds
> IN({$user->allowedBreeds()})' when doing a '$dogs->fetchAll()'.
>
> This allowed me to deal with 'hidden' records and a detailed security model
> without re-specifying the rules everywhere in my application - it saved my alot
> of time and testing.
>
> cheers,
> Peter
It seems to me that this would really be *your ORM* ;)
Personally, I don't see how your requirements conform with a KISS
principle. However, possibly you could achieve your goals by extending
overriding Zend_Db_Table's methods in your own *_Table classes.

Regards,

Victor


smime.p7s (4K) Download Attachment
Peter Hodge-5

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

Reply Threaded More More options
Print post
Permalink
>
> It seems to me that this would really be *your ORM* ;)
> Personally, I don't see how your requirements conform with a KISS
> principle. However, possibly you could achieve your goals by extending
> overriding Zend_Db_Table's methods in your own *_Table classes.
>

No, it's very much KISS (to use), and yes, it was done through inheritance.  It
looks something like this:
 
  // ORM class for the table 'dogs'
  class Table_dogs extends MyORM_Table {
    var $table = 'dogs';
    var $primary = 'DogID';

    function __construct() {
      parent::__construct();

      // if user is not 'super', only allow certain breeds
      global $user;
      if(!$user->isSuperUser()) {
        // JOIN breeds USING(DogID)
        $this->join('breeds')->using('DogID');

        // only allow the correct certain breeds:
        $this->where("breeds.BreedID IN({$user->breeds()})");
      }
    }
  }


I can't think of an easier way to add blanket rules which apply to an entire
project.

regards,
Peter



               
____________________________________________________
On Yahoo!7
Photos: Unlimited free storage – keep all your photos in one place!
http://au.photos.yahoo.com 

Victor Bolshov

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

Reply Threaded More More options
Print post
Permalink
Peter Hodge wrote:

>
> No, it's very much KISS (to use), and yes, it was done through inheritance.  It
> looks something like this:
>  
>   // ORM class for the table 'dogs'
>   class Table_dogs extends MyORM_Table {
>     var $table = 'dogs';
>     var $primary = 'DogID';
>
>     function __construct() {
>       parent::__construct();
>
>       // if user is not 'super', only allow certain breeds
>       global $user;
>       if(!$user->isSuperUser()) {
>         // JOIN breeds USING(DogID)
>         $this->join('breeds')->using('DogID');
>
>         // only allow the correct certain breeds:
>         $this->where("breeds.BreedID IN({$user->breeds()})");
>       }
>     }
>   }
>
>
> I can't think of an easier way to add blanket rules which apply to an entire
> project.
>
> regards,
> Peter
Hi Peter.

Possibly, in *your* case, this could be a good decision. But, at least,
it breaks the Single Responsibility Principle, and I'm quite sure that
code responsible for ACL features will need to be duplicated every time
you'd want to use those features.

PS. I also think this discussion does not fall into "KISS ORM system
upon Zend_Db_Table" thread.

Best regards,

Victor


smime.p7s (4K) Download Attachment