[eluser]OverZealous[/eluser]
DataMapper OverZealous Edition (DMZ) 1.2!
For those of you who haven't been following along, I have been making changes to the core of DataMapper that hopefully will help improve DataMapper's usability. The new features include:
• One-to-N relationships without a Join Table
• Multiple Relationships to the same object type
• Better Self References
• Include the fields from a related object in a result
Recently, I have decided to tackle one of those annoyances that has been requested several times: Accessing
extra information stored on a join table!
I simplified the problem a bit, so that, instead of supporting a whole range of features, I only support changing, viewing, and querying on extra columns that exist on join tables.
Some basic examples
First, assume I have two objects,
User and
Alarm. Each alarm can be related to multiple users, and each user can have multiple alarms. Suppose when a user logs in, or refreshes the page, (or some fantastical AJAX query runs), any current or overdue alarms are shown to the user so they can react.
We want to keep track of which user has seen which alarm, so we don't show it to the same user more than once. How do we keep track of this? By adding a column to
alarms_users called
viewed, which defaults to FALSE.
Here's some examples of how to work with it:
Code:
// get all non-viewed alarms for this user (you'd probably want to add in something to check the timestamp, as well)
// note: we have to tell the Alarm which table and object id to look at, that's why $user is passed in again
$user->alarm->where_join_field($user, 'viewed', FALSE)->get();
foreach($user->alarm->all as $a) {
echo($a->message);
// mark this alarm as viewed.
$a->set_join_field($user, 'viewed', TRUE);
}
// Query all alarms for this user, and include any extra join columns
$user->alarm->include_join_fields()->get();
echo("<ul>");
foreach($user->alarm->all as $a) {
$has = $a->join_viewed ? 'has' : 'has not';
echo("<li>Alarm {$a->id} $has been viewed.</li>");
}
echo("</ul>");
If you want to try the new features, the attached file includes a replacement for DataMapper - simply replace the copy you have in
libraries. There also is a slightly improved document explaining how the new features work, with more examples and less technical information. It isn't perfect, because I'm limited in how much time I can provide for this, but hopefully it will get you jump-started.