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

Matt S Trout dbix-class at trout.me.uk
Mon Dec 12 20:51:03 CET 2005


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.
 
> My packages look like this:
> package LicServ::License;
> LicServ::License->table('license');
> LicServ::License->add_columns(qw(id created assigned));
> LicServ::License->set_primary_key('id');
> LicServ::License->has_many('products' => 'LicServ::Product',
> {'foreign.license_id' => 'self.id'});
> 
> 
> package LicServ::Product;
> LicServ::Product->table('product');
> LicServ::Product->add_columns(qw(id activated created type description
> duration arcode license_id));
> LicServ::Product->set_primary_key('id');
> 
> 
> 
> -- 
> 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
> 
> 
> 
> _______________________________________________
> 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/

-- 
     Matt S Trout       Offering custom development, consultancy and support
  Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list