extra constraints on has_many relationship (patch)

[prev] [thread] [next] [Date index for 2004/06/08]

From: cees-cdbi
Subject: extra constraints on has_many relationship (patch)
Date: 20:15 on 08 Jun 2004
This message is in MIME format.

---MOQ10867257241fd5125fe3d26a5e121370c2f8ab1fac
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit


I would like some comments on a small patch I have attached that will allow
extra constraints to be included in has_many relationships.  This addition can
simplify some common situations.

Say we have a standard domains and users table setup, where a domain has_many
users.

My::Domains->has_many(users => 'My::Users');

Quite often you will have a field in the users table stating whether the
account is active or not.  So to get a list of all active accounts you would do
the following:

my @users = $domain->users(active => 1);

If this is done often enough, it would be easier (and less prone to error or
ommission) if we had another method called activeusers that achieved the same
thing.

My::Domains->has_many(users => 'My::Users');
My::Domains->has_many(activeusers => 'My::Users', { constraint => [active =>
1]);

my @users = $domain->activeusers();

Now you can call ->users or ->activeusers depending on what you are after.

The included patch only adds 1 line of code to Class::DBI::Relationship::HasMany
 to implement this (which is a testament to the implementation of Class::DBI). 
It does add a bit of complexity to the has_many method, but only if one is
interested in using this feature.

Of course there are probably other ways to achieve the above results, but this
seems to me like a simple and clean way of handling a common situation.

Comments/Questions?

Cees

---MOQ10867257241fd5125fe3d26a5e121370c2f8ab1fac
Content-Type: application/octet-stream; name="hasmany_constraints.patch"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="hasmany_constraints.patch"

ZGlmZiAtcnUgQ2xhc3MtREJJLTAuOTYub3JpZy9saWIvQ2xhc3MvREJJL1JlbGF0aW9uc2hpcC9I
YXNNYW55LnBtIENsYXNzLURCSS0wLjk2L2xpYi9DbGFzcy9EQkkvUmVsYXRpb25zaGlwL0hhc01h
bnkucG0KLS0tIENsYXNzLURCSS0wLjk2Lm9yaWcvbGliL0NsYXNzL0RCSS9SZWxhdGlvbnNoaXAv
SGFzTWFueS5wbQkyMDA0LTA0LTI1IDExOjMzOjM2LjAwMDAwMDAwMCAtMDQwMAorKysgQ2xhc3Mt
REJ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XRp
b25hbCBrZXkvdmFsdWUgcGFpcnMgZm9yIHJlc3RyaWN0aW5nIHRoZSBzZWFyY2guIFRoZSBhYm92
ZSBleGFtcGxlCiB3aWxsIG9ubHkgcmV0dXJuIHRoZSBDRHMgd2l0aCBhIHllYXIgb2YgMTk4MC4K
IAorCU11c2ljOjpBcnRpc3QtPmhhc19tYW55KHJvY2tjZHMgPT4gJ011c2ljOjpDRCcsIHsgY29u
c3RyYWludCA9PiBbIGdlbnJlID0+ICdyb2NrJyBdIH0pOworCW15IEBjZHMgPSAkYXJ0aXN0LT5y
b2NrY2RzKHllYXIgPT4gMTk4MCk7CisKK1lvdSBjYW4gYWxzbyBjb25zdHJhaW4gdGhlIHJlbGF0
aW9uc2hpcCB0byBvbmx5IGluY2x1ZGUgYSBzdWJzZXQgb2YgdGhlIAorYXZhaWxhYmxlIHJlY29y
ZHMgYnkgcHJvdmlkaW5nIGEgJ2NvbnN0cmFpbnQnIG9wdGlvbiB0byB0aGUgaGFzX21hbnkKK3Jl
bGF0aW9uc2hpcC4gIFRoaXMgd2lsbCBhdXRvbWF0aWNhbGx5IGFkZCB0aGUgY29uc3RyYWludCBp
bnRvIHRoZSB3aGVyZQorY2xhdXNlIHdoZW4gZG9pbmcgYSBzZWFyY2guCisKID1oZWFkMyBPcmRl
cmluZwogCiAJTXVzaWM6OkNELT5oYXNfbWFueSh0cmFja3MgPT4gJ011c2ljOjpUcmFjaycsIHsg
b3JkZXJfYnkgPT4gJ3BsYXlvcmRlcicgfSk7Cg==

---MOQ10867257241fd5125fe3d26a5e121370c2f8ab1fac--

extra constraints on has_many relationship (patch)
cees-cdbi 20:15 on 08 Jun 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52