[Dbix-class] more complex relationships
Jess Robinson
castaway at desert-island.demon.co.uk
Fri Mar 24 14:29:05 CET 2006
I'd still like to see the DBIx::Class code..
So, if I read it correctly now, from sale_item, which has a sale_id and a
product_d, you want to get the entry in vendor_product?
$schema->resultset('VendorProduct')->find({vendor_name =>
$saleitem->sale->vendor_name,
product_id =>
$saleitem->product_id});
?
.. so it wants a sort of infinite level relationship search similar to how
we do it with joins already?
DB::Schema::SaleItem->add_relationship('vendor_products',
{ 'sale.sale_id' => 'self.sale_id',
'vendor_product.vendor_name' => 'sale.vendor_name',
'vendor_product.product_id' => 'self.product_id',
},
{ from =>
[ {},
{ 'vendor_product.vendor_name' => 'sale.vendor_name' },
[ {},
{ 'vendor_product.product_id' => 'self.product_id' },
[ {},
{ 'sale.sale_id' => 'self.sale_id' }
]
]
]
}
);
I'm making this up as I go along, so I hope someone who understands how
FROM works will correct me.. I think I grasp the essence now tho..
Jess
On Thu, 23 Mar 2006, Mark Hedges wrote:
>
>
> On Thu, 23 Mar 2006, Jess Robinson wrote:
>>
>> I just spent several minutes writing a DBIx::Class schema to handle this,
>> and now it's occured to me that we haven't seen what you've tried..
>>
>> It certainly doesn't look hard at all, so I have a suspicion that you just
>> got your relationships mixed up, can you show the code you tried?
>>
>> Also, whats a product_id? It seems to just exist in vendor_product's
>> imagination.. and sale_item has one as well, but I have no idea where they
>> are pointing.
>>
>> .. Anyway, to get a vendor_name from a sale_item, you need to do something
>> like:
>>
>> DB::Schema::SaleItem->belongs_to(sale_id => 'DB::Schema::Sale');
>> DB::Schema::Sale->belongs_to(vendor_name => 'DB::Schema::Vendor');
>>
>> then from a $saleitem:
>>
>> $saleitem->sale_id->vendor_name
>>
>> will get you a vendor object
>>
>> (You can name the relationships however you like)
>
> A vendor_product's product_id is the product_id at the remote
> vendor for one of our products that we might be selling there.
> We might sell the same product in multiple ways/prices with one
> vendor. product_id is arbitrary, it comes from the vendor, but
> for every vendor_product, the MCPK of (vendor_name, product_id)
> will be unique and defines what it is.
>
> Your example is obvious, but not what I am trying to do. I want
> to go from a sale_item to the vendor_product that was sold. I
> can't without writing a subroutine for a sale_item to get the
> vendor_name from the parent sale, then look up the
> vendor_product from that vendor_name and its product_id.
>
> Mark
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
>
>
More information about the Dbix-class
mailing list