[Dbix-class] has_a mutator: set_rel_single

Sam Vilain sam at vilain.net
Sun Jul 31 04:45:22 CEST 2005


David Kamholz wrote:
> There are a number of possibilities for how we want this mutator to  
> behave. First of all, it's important to correctly conceptualize the  
> has_a relationship (and separate it from might_have, must_have, is_a,  

Another problem already solved in Tangram / Class::Tangram.

Consider the following schema;

   my $schema = { classes => [
                    Species => {
                      fields => {
                        ref => {
                          genus => {
                            class => "Genus",
                            companion => "species",
                          } } },
                    },
                    Genus => {
                      fields => {
                        iset => {
                          species => {
                            class => "Species",
                            companion => "genus",
                            coll => "genus", # specify the column
                          } } },
                    }
                  ] };
   Class::Tangram::Generator->new($schema);
   my $tangram_schema = Tangram::Schema->new($schema);

> (1) The user would like to assign the species to a new genus. This  
> involves inserting a row into the genus table, possibly with relevant  
> data to fill in the rows, and storing the key in the species table.

   $species->set_genus($new_genus);
   $storage->update($species);

If $new_genus is not already in the database it will be added.

> (2) The user would like to reassign the species to a different genus.  
> This involves changing the key in the species row to refer to a  
> different genus row.

Same thing;

   $species->set_genus($new_genus);
   $storage->update($species);

 > (3) The user would like to modify something in the genus row.

As you mentioned, the third problem is very simple;

   $species->genus->set_something(...);
   $storage->update($species->genus);

You don't _need_ Class::Tangram (the accessors package) for this to
work; however the benefits are that the in-memory representations will
be automatically updated via the "companion" feature.

Sam.



More information about the Dbix-class mailing list