The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

It would be good if a sub-forum could be requested on this forum, where we could have each PHP Pattern in their own threads. That way discussions could continue within the forum and everything would be well organised and not all muddled up: and it would be easy to link to if questions came up during forum conversations.

and the call will automatically be proxied to the SomeClass instantance stored in SomeClassProxy::$subject.

A more typical example for a Proxy class might be to have some kind of an access control object as part of the Proxy construction. You could then write a Model class without worrying about whether a user is authorized to do any of the actions the Model provides. Whenever you use the Model, you could then use it through a Proxy object which would automatically check if the user is authorized before providing access to the proxied Model class.

If the classes of your domain model and the structures of your sql tables match closely (which they often do), then you can take that to your advantage. An Active Record is an object - a part of the domain model - that is responsible for itself whether it is inserted into the db, deleted or updated. So unlike you have a central DataMapper approach for inserting/updating and deleting data, you can save some code, when you do it with the Active Record.

An ActiveRecord is typicially responsible for the following:
* Construct an instance of the Active REcord class from a SQL result set row
* Construct a new instance for later insertion into the table
* Either static finder methods or centralized Finder classes are used for fetching SQL result set..the Active Record is mostly used only for modifying data. Whatever way you take, the fetching mechanisms should return ActiveRecord objects.
* Update the Database and insert into it the data from the Active Record (every attribute becomes one column in the SQL row)
* Get and set the fields

I think this pattern is pretty straight-forward. I can write some example code, though, if requested.

//how would you check it by hand? do it in the test!
$alt_test = $this->conn->getOne(
"select id from bookmark where url = 'http://phparch.com'");
$this->assertEqual(2, $alt_test);
//alternatively
$this->assertEqual($link->getId(), $alt_test);
}

No, this thread has fallen by the way side by the looks of things; At the time it was a good idea, as fewer people really knew enough about patterns, plus a couple of years ago there was little documentation in regards to PHP and patterns.

That isn't the case today, where there are more plentiful resources; Did you have any particular patterns in mind that you wanted to know more about?

Thank, I have a questions regarding Active Record & Table Data Gateway, So this is what I understand: the Active Record pattern deals with a single record in a table and the Table Data Gateway deals with multiple records. How can I use them together effectively? For example, A User class abstracts the user table and provides some domain logic. If I want to retrieve multiple records at a time, how can I get a multiple records of User object so that I can make use of the domain logic.
Is this how it works?

Thank, I have a questions regarding Active Record & Table Data Gateway, So this is what I understand: the Active Record pattern deals with a single record in a table and the Table Data Gateway deals with multiple records. How can I use them together effectively? For example, A User class abstracts the user table and provides some domain logic. If I want to retrieve multiple records at a time, how can I get a multiple records of User object so that I can make use of the domain logic.
Is this how it works?

You can have a domain object which essentially equates to a database row with both patterns, the key distinction is where the actual logic which accesses the database itself resides. In the ActiveRecord, this logic is contained in the row level object itself, whereas the TableDataGateway has the database logic and the can return collections of (or individual) domain objects, which still can have business logic, but would need to ask the TableDataGateway to persist any changes to themselves in the database.

> but would need to ask the TableDataGateway to persist any changes to themselves in the
> database.

So, with the Active Record you would do this,

PHP Code:

// ...$record -> save();

Whereas, with Jason's TableDataGateway (from my understanding of course) you would do this instead,

PHP Code:

// ...$gateway -> save( $record );

Where the Gateway in question would have the knowledge to know how to save the record for example. Is this what you had in mind yourself?

Personally I would one or the other but not both at the same time, as I believe therefore if you used the Gateway option, the Active Record (from my viewpoint) would be bloated, having to carry around additional responsibilities, which would be taken care of anyways by the Gateway.

Dr. L, take my User class as example again, if this class defines some business logic like: is_vip_user(), get_estimated_income(), wouldn't you want to reuse this code in the active record instead of redefining them in the gateway.

> wouldn't you want to reuse this code in the active record instead of redefining them in the
> gateway.

Yes. The logic in this case is in regards to that one row of data that is likely to be unique; The emphasis is with the data therefore and belongs with that row of data. From my point of view, this would be the case where you have that uniqueness so any behaviour you have acts on that row of data only.