Skip to end of metadata
Go to start of metadata
Under Construction
This proposal is under construction and is not ready for review.

Zend Framework: Zend_Db_ActiveRecord Component Proposal

Proposed Component Name Zend_Db_ActiveRecord
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_Db_ActiveRecord
Proposers Ken Chau
Revision 1.0 - 03/05/2008: Initial draft (wiki revision: 24)

Table of Contents

1. Overview

Zend_Db_ActiveRecord is an implementation of the ActiveRecord pattern based on the Patterns of Enterprise Application Architecture book by Martin Fowler. It also has similar functionality as the Ruby on Rails ActiveRecord implementation. This implementation relies on PHP 5.3's late static binding capability (currently not in stable yet).

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

An implementation of ActiveRecord according to PoEAA (Fowler, 161) should have the following characteristics:

  • Construct an instance of the Active Record from a SQL result set row
  • Construct a new instance for later insertion into table
  • Static finder methods to wrap commonly used SQL queries and return Active Record objects
  • Update the database and insert into it the data in the Active Record
  • Get and set the fields
  • Implement some pieces of business logic

In the Zend Framework context, here are the requirements:

  • This component will perform CRUD operations on a table mapped to an ActiveRecord object.
  • This component will use existing Zend_Db for all actual database operations.
  • This component will use static find methods and treat instantiations to be an object representation of a table row.
  • This component will allow for global default configuration much like Zend_Db_Table_Abstract.
  • This component will work with validators and filters as "some pieces of business logic"

4. Dependencies on Other Framework Components

  • Zend_Exception
  • Zend_Db
  • Zend_Db_Select

5. Theory of Operation

6. Milestones / Tasks

  • Milestone 1: design notes will be published here
  • Milestone 2: Working prototype checked into the incubator supporting use cases #1, #2, ...
  • Milestone 3: Working prototype checked into the incubator supporting use cases #3 and #4.
  • Milestone 4: Unit tests exist, work, and are checked into SVN.
  • Milestone 5: Initial documentation exists.

7. Class Index

  • Zend_Db_ActiveRecord_Abstract

8. Use Cases

UC-01

Declaring an ActiveRecord Model, defining Relationships

UC-02

Create, Update, Delete

UC-03

Find a record in the table by ID and by where clause

UC-04

Find records in the table by a custom SQL

UC-05

One-to-one relationship

In this example use case, a Driver owns a Car and only one Car. Note that the foreign key field exists on the owned item.

UC-06

One-to-many relationship

UC-07

Many-to-many relationship

9. Class Skeletons

Labels:
zend_db_activerecord zend_db_activerecord Delete
proposals proposals Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 06, 2008

    Woohoo!

  2. Mar 09, 2008

    How to you plan to implement the typical static find-calls? Have you thought about __callStatic()?

    1. Mar 09, 2008

      That's exactly how I expect to have find calls to be implemented. PHP 5.3 constructs used will be:

      • the static::$properties
      • get_called_class()
      • __callStatic($method, $argument)
  3. Mar 09, 2008

    I love this.........
    haha just like ruby on rail...

    but why must using php 5.3? 

    1. Mar 09, 2008

      Late static binding. I updated the proposal to clarify this.

  4. Mar 13, 2008

    I personnaly do love this pattern (ActiveRecord) ; anyhow, I'm wondering if two so close but so different patterns (ActiveRecord / DB Table Row Gateway) implemented in the same framework is not too much...

    Still, I would love such a component

  5. Jun 01, 2008

    This looks quite good... but... I don't want to sound rude or anything, but wouldn't it be more beneficial for everyone to concentrate efforts on a single ORM library, like Doctrine?

  6. Jun 20, 2008

    Wouldn't it be better to handle the relationsships in an protected variable $_relations, $_relationsships or something alike, rather than an initialization function (maybe making init function optional).

    Relationsships are more an description of an object rather than something that has to be "initialized" in a function.

    But maybe this is just the request of a nitpicker...

    Also there is currently no UseCase for the integration of Validators on certain ActiveRecord fields, I would like to see how that might be integrated and configured.

  7. Jun 21, 2008

    Hi Ken, if you want to start working with this before 5.3, I've found an alternative solution for those static calls:

    Hope it helps,

    Aldemar

    1. Jun 21, 2008

      The problem is in how you can get the class name in a static method from the parent. So, get_static_class() is the functionality that is really truly missing here.

      1. Jun 21, 2008

        Ken, this is exactly what I'm using right now in my active record approach and works perfect. Since the init method returns already an instance of Person and not Zend_Db_ActiveRecord_Abstract, calling from the parent any function using $this (meaning not static call) will use Person and then Zend_Db_ActiveRecord_Abstract.

    2. Jun 21, 2008

      The truly viable way to do this is to do something like this:

      I guess I can create something like this for < 5.3 as well.

      1. Jun 21, 2008

        IMHO, I prefer:

        instead of:

        The first one will reflect better how it's actually going to work, btw, in my implementation I have an interface with that init() method defined, so, that ensures that every class you create has the init() method.

        1. Jul 23, 2008

          You don't need an interface for that, simply make the class you are extending abstract and define it as an abstract method.

  8. Jul 29, 2008

    I wanna that you think about inheritance. Hibernate have 3 way to make it, for 3 different environment. The Joined inheritance is most common. The way consist in 2 classes in your project and you have 2 tables in database scheme. I wanna a code like this:

    This code reflect my intention. So think about this.

    Regards

  9. Jul 29, 2008

    I wanna that you think about inheritance. Hibernate have 3 way to make it, for 3 different environment. The Joined inheritance is most common. The way consist in 2 classes in your project and you have 2 tables in database scheme. I wanna a code like this:

    SQL create table scheme.

    PHP code wanted

    This code reflect my intention. So think about this.

    Regards

  10. Jun 22, 2009

    Hi Ken,

    I have implemented similar thing in my project, and it is a a huge help for new programmers. It is so easy for them to do the regular operations.

    I have created table classes and used setters and getters, and a base class that mimics the active record from ruby on rails as you have indicated.

    Let me know if I can help in anyway to improvise this component or if you would like to see how I have done it.

    Regards,
    Sawan

  11. Dec 07, 2010

    Hi Ken! Are there some news about this proposal? Do you work now on it's implementation?

    Regards, Alex.

    1. Dec 08, 2010

      Alex,

      This proposal is dead; it is not actively being developed. It can safely be archived.

      -Matt (works with Ken)

      1. Dec 08, 2010

        Sadly... What reasons are there (tech, architect, or just spare time)?

  12. Jan 19, 2011

    I know this is dead right now but I personally hope it gets implemented. I have been researching the benefits of different database access patterns for my web app and I would REALLY like to use the Active Record model. I am going to write my own abstract class to make it work, but it would have been so much nicer if ZF supported this.

    I've also been researching a bunch on RoR and what I've realized is that one of RoR's strongest features is the Active Record support. In other words, I feel that ZF should invest in this in order to compete with RoR. In my opinion the ZF is so good and widely used because it is so extendable. RoR's strong points are speed. ZF needs to continue with its extendibility and implement the ActiveRecord pattern.

    1. Jan 20, 2011

      If you want Active Record use Doctrine 1. Doctrine 2 on the other hand uses a data mapper pattern, maybe you would want to look at that one too.

  13. Feb 05, 2011

    Archiving this proposal, feel free to recover it when you want to work on it again. For more details see this email.