Re: Feature request - Allow searching columns of related tables in search() and search_like()

[prev] [thread] [next] [Date index for 2005/01/10]

From: Tony Bowden
Subject: Re: Feature request - Allow searching columns of related tables in search() and search_like()
Date: 12:21 on 10 Jan 2005
On Mon, Jan 10, 2005 at 11:46:47AM +0000, info@xxxxxx.xx.xx wrote:
> I need to search for an Order object via a field of a related object,
> for example a Customer.surname. So if writing an SQL statement my SQL
> would be:
> SELECT * FROM order, customer WHERE order.customer = customer.id AND customer.surname = ...

Will there just be one customer matching your query? Or perhaps many?

If it's just one you could do:

my @orders = Customer->search(surname => $surname)->first->orders;

If multiple,

my @orders = map $_->orders, Customer->search(surname => $surname);

These assume, of course, that have the relevant has_a and has_many:

Order->has_a(customer => 'Customer').
Customer->has_many(orders => 'Order').


The first will be quite efficient. The second depends on how many
customers will match, but that's a non-standard approach. The normal
approach would be to pass arguments to the orders() call to restrict the
orders belonging to the customer:

  $customer->orders(year => 2003);

Tony

(message missing)

Re: Feature request - Allow searching columns of related tables in search() and search_like()
Tony Bowden 12:21 on 10 Jan 2005

Generated at 12:48 on 22 Feb 2005 by mariachi v0.52