On 12/12/06, Christopher H. Laco <[EMAIL PROTECTED]> wrote:
> Classic example, cart and cart_item tables in a one to many. I have a
> relationship 'items' setup, and it returns all of the carts items using
> $cart->items...but for the life of me, I can't figure out how to only
> return some of the items [...]
>
> Can this be done with the auto generated items() relationship accessor?
> Or it it better to use an manager to only load items for the current
> carts id?
Relationships accessors in RDBO are not (just) a means to fetch
related items. They represent a well-defined set of items. You can
get them, yes, but you can also add to them or set them:
@items = $cart->items;
@new_items = mangle_items_in_some_way(@items);
$cart->items(@new_items);
$cart->save; # new items saved here, replacing old items
(You can nest this kind of thing arbitrarily deep, with the top-level
save() cascading as needed to preserve referential integrity.)
You can see how this view of related objects is not compatible with
the view of relationship accessors a means to fetch related objects
according to criteria that can change on a per-call basis:
# Get items whose names start with A
@a_items = $cart->items(filter => [ name => { like => 'A%' } ]);
@new_a_items = mangle_items_in_some_way(@a_items);
$cart->items(@new_a_items);
$cart->save; # Er, what happens here?
If you want to do ad-hoc queries with filters, either use the Manager
directly, or create your own object methods that wrap the appropriate
manager calls:
package Cart;
...
sub get_items
{
my($self, %args) = @_;
# Clause that links items to this cart
push(@{$args{'query'}}, cart_id => $self->id);
return CartItem::Manager->get_items(%args)
}
Note that this is a fetch-only method. Then you can do this:
@a_items = $cart->get_items(query => [ name => { like => 'A%' } ]);
This kind of method is useful enough that I plan to add a new method
type for it, probably called get_filtered or some such. But for now,
doing it manually (or automating it in a base class) is
straightforward.
-John
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object