On 1/2/08 5:45 PM, Justin Ellison wrote:
> We have SKU's, and each row in the SKU table might or might not have a
> corresponding entry in the INVENTORY table.  The gotcha here is that the
> INVENTORY table lives in a different schema than does the SKU table.
> Now, I can get at the inventory "indirectly" by manually creating a new
> Inventory object, but I can't get at it using the relationship in SKU.
> The problem is that RDBO is using the schema name from My::SKU to query
> the associated rows in INVENTORY.  I'll be the first to admit that I'm
> pretty green to all this, so it's probably an error in my code, but I've
> tinkered with different things for a few hours now and can't seem to get
> it right.

If you want to do cross-schema relationships, you have to make the schema
differ per-class instead of per-data-source.  (The parent object's database
connection will be used to fetch child items; I could add more flexibility
for this in the future, but it can get pretty hairy with transactions and
such...)

So, register a single data source instead of two:

> __PACKAGE__->register_db(
>       domain   => 'pts',
>       type     => 'main',
>       ...);

but then override the schema in the inventory class:

> package My::Inventory;
> 
> use base qw(Rose::DB::Object);
> 
> __PACKAGE__->meta->setup(
>     table       => 'inventory',
>     ...
>     primary_key_columns  => [ qw(sku_code) ],
>
>     schema => 'b_custom',
> );

That should do it.  Also, you may want to reconsider doing this in every
class:

> our $DB;
> 
> sub init_db {
>   $DB ||= My::DB->new;
> }

When this code appears in every class, it means that all objects of a given
class will share a single connection.  That's an odd arrangement and perhaps
not what you're aiming for :)

If you want to share a Rose::DB object among all your
Rose::DB::Object-derived classes, consider using Rose::DB's new_or_cached()
method.

http://search.cpan.org/dist/Rose-DB/lib/Rose/DB.pm#new_or_cached

To share db objects only under mod_perl, this arrangement works well:

http://search.cpan.org/~jsiracusa/Rose-DB-0.737/lib/Rose/DB/Cache.pm#prepare
_db

Either way, since all your classes are now using a single data source
("pts"/"main") you should only need a single implementation of init_db() in
your common base class (rather than repeated code in each RDBO-derived
class).

-John



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to