On 10/18/05, John Siracusa <[EMAIL PROTECTED]> wrote:
> So while you can reduce, say, the vendor class to this:
>
> package My::Vendor;
> use base 'My::Object';
> __PACKAGE__->meta->auto_initialize;
>
> and it works fine, once you start stripping down the classes that participate
> in the product/colors many-to-many relationship in a similar manner, you
> eventually run out of DWIMery.
I didn't like that too much so I added more DWIMery :) I'm using the
same schema as posted before (appears at the end of this email). Now
look at these minimalist classes:
---
package My::Auto::Color;
use base 'My::Object';
__PACKAGE__->meta->auto_initialize;
---
package My::Auto::Price;
use base 'My::Object';
__PACKAGE__->meta->auto_initialize;
---
package My::Auto::ProductColors;
use base 'My::Object';
__PACKAGE__->meta->auto_initialize;
---
package My::Auto::Vendor;
use base 'My::Object';
__PACKAGE__->meta->auto_initialize;
---
package My::Auto::Product;
use base 'My::Object';
__PACKAGE__->meta->relationships
(
prices => { type => 'one to many' },
colors => { type => 'many to many' },
);
__PACKAGE__->meta->auto_initialize;
---
I don't think I can DWIM much more than that! But remember, this
example relies upon the database having real foreign key support.
Otherwise I can't auto-init foreign keys, and then the relationships
like many-to-many can't find those keys to auto-config their mapping
tables, etc. etc.
Anyway, sample usage:
$p = My::Auto::Product->new(id => 1)->load;
# Object via foreign key (many to one)
print $p->vendor->name;
# Objects via foreign key (one to many)
print join(', ', map { $_->region . ': ' . $_->price } $p->prices);
# Objects via mapping table (many to many)
print join(', ', map { $_->name } $p->colors);
The schema appears below, and I added a new script to actually run the
code above:
/rose/modules/Rose-DB-Object/t/sandbox/convention/convention-test-auto.pl
All of this is in CVS now.
-John
---
CREATE TABLE vendors
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE colors
(
code CHAR(3) NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE products
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255),
vendor_id INT NOT NULL REFERENCES vendors (id)
);
CREATE TABLE prices
(
price_id SERIAL NOT NULL PRIMARY KEY,
product_id INT NOT NULL REFERENCES products (id),
region CHAR(2) NOT NULL DEFAULT 'US',
price DECIMAL(10,2) NOT NULL
);
CREATE TABLE product_colors
(
id SERIAL NOT NULL PRIMARY KEY,
product_id INT NOT NULL REFERENCES products (id),
color_code CHAR(3) NOT NULL REFERENCES colors (code)
);
---
-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object