Re: [RDBO] Defining Some times Relationships

2007-03-27 Thread John Siracusa
On 3/27/07, Robert James Kaes [EMAIL PROTECTED] wrote:
 CREATE TABLE locations (
 id  INT,
 ...);

 CREATE TABLE companies (
 id   INT,
 location_id  INT NOT NULL REFERENCES locations(id),
 ...);

 CREATE TABLE exhibitions (
 id   INT,
 location_id  INT NOT NULL REFERENCES locations(id),
 ...);

 [...] What I'd like to create is a company relationship in Exhibition that
 returns the Company that has the same location_id as itself.

Given the schema above, there could be many such companies, not just
one.  So it'd be a one-to-many relationship:

package Exhibition;
...
__PACKAGE__-meta-setup(
  ...
  relationships =
  [
companies =
{
  type   = 'one to many',
  class  = 'Company',
  column_map = { location_id = location_id },
},
...

You'll get a list or reference to an array of related companies
through this method:

$e = Exhibition-new(id = ...)-load;
$companies = $e-companies;

if(@$companies) { ... }

If there really can only be one such company with the same location
id, then you'll have to make location_id a unique key of the companies
table and/or Company class:

package Company;
...
__PACKAGE__-meta-setup(
  ...
  unique_key = 'location_id',
  ...

then set up a many-to-one relationship like this:

package Exhibition;
...
__PACKAGE__-meta-setup(
  ...
  relationships =
  [
company =
{
  type   = 'many to one',
  class  = 'Company',
  column_map = { location_id = location_id },
  required = 0,
},
...

The required attribute makes the method return undef (instead of
throwing an exception) when no matching company exists:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadata/Relationship/ManyToOne.pm#required

$company = $e-company; # may be undef

-John

-
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT  business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.phpp=sourceforgeCID=DEVDEV
___
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object


Re: [RDBO] Defining Some times Relationships

2007-03-27 Thread Robert James Kaes
On Tue, 27 Mar 2007, John Siracusa wrote:
 If there really can only be one such company with the same location
 id, then you'll have to make location_id a unique key of the companies
 table and/or Company class:

 The required attribute makes the method return undef (instead of
 throwing an exception) when no matching company exists:
 
 http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadata/Relationship/ManyToOne.pm#required
 
 $company = $e-company; # may be undef

John,
Thank you!  This was exactly what I was looking for.
-- Robert

-- 
Robert James Kaes  WormBytes Consulting and Contracting
http://tinyproxy.sourceforge.net/  http://www.wormbytes.ca/

-
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT  business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.phpp=sourceforgeCID=DEVDEV
___
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object