For the record, I am just forwarding a reply to an email that JohnS kindly
sent to me in reply to a question about the Rose Loader.


> Also, once I've auto-initialised a package/table using Loader, presumably
it won't know
> the relationships (under MySQL).  Can I add the relationships afterwards
in
> a convenient way?

There are a few ways to do this.  If you're auto-initializing from
within the specific .pm file for that class, the best way to do it is
to add the relationships right there:

    # File: My/Product.pm
    ...
    __PACKAGE__->meta->setup
    (
      # Set table if it can't be derived from the class name
      table => 'prods',

      # Fill in relationships that can't be determined by looking
      # at the database itself (e.g., in MySQL ISAM)
      relationships => [ ... ],

      # Automatically fill-in the rest
      auto => 1,
    );

If you're using the Loader instead, check out the post_init_hook
attribute.  Use it to load the appropriate .pm file to augment the
auto-created classes after the fact.

    $loader->post_init_hook(sub
    {
      my $meta = shift;
      # For class My::Product, try to load My::Product::Extra
      my $extra_pm = $meta->class . '::Extra';
      eval "require $extra_pm";
    });

    $loader->make_classes(...);

Then in My/Product/Extra.pm, do this:

    # Fill in relationships that can't be determined by looking
    # at the database itself (e.g., in MySQL ISAM)
    My::Product->meta->add_relationships(...);

    # Re-initialize, preserving existing (auto-inited) metadata
    My::Product->meta->initialize(preserve_existing => 1);

    1;

Of course, if you don't have any customization to do, you can choose
not to create an Extra.pm file for a given class.

There are variations on this theme.  You could also use the Loader's
make_modules() method to generate .pm files and then edit those either
manually or programmatically.  The later is kind of scary, source
filtering and all, but you could also just append the equivalent of
the contents of the sample My/Product/Extra.pmExtra.pm above.

In all these "auto-then-some-fixups" scenarios, there can be
chicken/egg situations lurking.  That's why I favor auto-generating
.pm files once and then editing them manually from then on.  Also
remember the ...->meta->perl_class_definition() method when you want
to see a class that was auto-generated for you.  It's a valuable
debugging tool:

    print My::Product->meta->perl_class_definition;

-John


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to