[Dbix-class] How to bring existing objects together?

Tilman Baumann tilman.baumann at collax.com
Tue Dec 13 14:22:29 CET 2005


Matt S Trout schrieb:

>On Mon, Dec 12, 2005 at 05:57:26PM +0100, Tilman Baumann wrote:
>  
>
>>Hi,
>>
>>i have two tables. Product may have a license related. (license_id on
>>table license)
>>
>>I like to add a existing product object to a license.
>>I guessed i can do something like this:
>>
>>$license->add_to_products($product);
>>Where $product is a instance of LicServ::License.
>>
>>But this does not work because add_to_products wants a HASH ref.
>>
>>Is there a nicer way to make such relations without seting the foreign
>>key on the product object to the primary key of the license object?
>>like this:
>>$product->license_id($license->id)
>>
>>Well add_something is not what i want to do. So i see why this does not
>>work. :)
>>But is there a way to bring two objects together which have defined
>>relationships?
>>    
>>
>
>If you add a belongs_to rel from product to license called 'license', you'll
>get a $product->license accessor for the license object. Then you just call
>
>$product->license($license)
>
>and DBIx::Class will handle it for you.
>  
>

Well this would be nice. A bit different than i expected. But it's logic. ;)

But i still have a little problem here.
The relation hasn't the same name as the refering column.
Strangely he belongs_to call do not fail. But if i call the
$product->license() function it fails because the fuction is not defined.

If i give the relation the same name as the column in the table it works
fine.
I think i have to give a condition too. But this is anythin i do not
understand completely yet.

I use license_id() now. Thats ok.

Nevertheless it would be nice if i can give the relation in the other
direction. Like add_to_relname().
Maybe the add_to_relname accessor needs to check if a parameter is of
type Foreign::Class and and do not try to create the object but only
link them toghether.
I find that verry usefull and it's what i first expected.... ;)

-- 
Tilman Baumann
Software Developer
Collax GmbH . Boetzinger Straße 60 . 79111 Freiburg . Germany

p: +49 (0) 761-4514-836
f: +49 (0) 761-4563-793


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/dbix-class/attachments/20051213/e64c628e/attachment.htm


More information about the Dbix-class mailing list