Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

DBIx::Class questions

by jimbus (Friar)
on Oct 11, 2006 at 14:44 UTC ( #577613=perlquestion: print w/ replies, xml ) Need Help??
jimbus has asked for the wisdom of the Perl Monks concerning the following question:

I'm defining some objects using DBIx::Class::Schema with the intention of using deploy to create my tables and have come up with a couple questions. These maybe just me doing something simple wrong, so please be patient... I'm in information overload, ATM. :)

1) When you are using has_one can you overload the child object name? For example, if I have an object called system and want to attach a point_of_contact, owner and vendor and they are all actually user objects. I've tried '__PACKAGE__->has_one(Point_of_Contact => 'NEOpS::WDS::Users');'
but that's not working. Any of the examples I've found just have simplest case usage with little or not extending of the concept.

2) I've been building this, from a directory point of view, in a very heirarchical manner. So for package NEOpS::WDS in the /lib/DB/NEOpS dir (this is in the catalyst framework dir stuc). There WDS.pm has

package NEOpS::WDS; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(); 1;
Then in the WDS dir, I have object_name.pm for each object I'm declaring. I've noticed that most of the examples I'm finding seem to be very flat by comparison... should I just to this all in the /NEOpS/WDS.pm?

Thanks,


--Jimbus aka Jim Babcock
Wireless Data Engineer and Geek Wannabe
jim-dot-babcock-at-usa-dot-com

2006-10-12 Retitled by castaway, as per Monastery guidelines
Original title: 'DBIx:questions'

Comment on DBIx::Class questions
Select or Download Code
Re: DBIx::Class questions
by jasonk (Parson) on Oct 11, 2006 at 15:50 UTC

    When you are using has_one can you overload the child object name?

    Yes, but if the accessor name is different from the column name, you have to specify both, so if you have colums in the current package called 'poc_id', 'owner_id', and 'vendor_id' that all refer to the uesrs table, then you would need something like this:

    __PACKAGE__->has_one( 'Point_of_Contact', 'NEOpS::WDS::Users', 'poc_id' ); __PACKAGE__->has_one( 'Owner', 'NEOpS::WDS::Users', 'owner_id', ); __PACKAGE__->has_one( 'Vendor', 'NEOpS::WDS::Users', 'vendor_id', );

    I've noticed that most of the examples I'm finding seem to be very flat by comparison... should I just to this all in the /NEOpS/WDS.pm

    I think thats primarily a personal preference call, although I prefer to do it the way you are, with each class in it's own file, in a traditional perl-style directory structure.

    And if you haven't already, check DBIx::Class::Relationship and DBIx::Class::Relationship::Base for the most complete (although not always clearest) docs on relationships.


    We're not surrounded, we're in a target-rich environment!
Re: DBIx::Class questions
by jimbus (Friar) on Oct 11, 2006 at 19:22 UTC

    Maybe I have a more basic misunderstanding... let me list my actual definitions and see what you think and I'll read other the documents suggested.

    WDS.pm

    package NEOpS::WDS; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(); 1;

    Systems.pm

    package NEOpS::WDS::Systems; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('systems'); __PACKAGE__->add_columns(obid => { accessor => 'elements', data_type => 'integer', size => 16, is_nullable => 0, is_auto_increment => 1, default_value => '', }, name => { data_type => 'varchar', size => 64, is_nullable => 1, is_auto_increment => 0, default_value => '', }, descrip => { data_type => 'varchar', size => 256, is_nullable => 1, is_auto_increment => 0, default_value => '', } ); __PACKAGE__->set_primary_key('obid'); __PACKAGE__->has_one('username', 'NEOpS::WDS::Users', 'Point_of_Contac +t' ); 1;

    Users.pm

    package NEOpS::WDS::Users; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('users'); __PACKAGE__->add_columns(obid=> { accessor => 'users', data_type => 'integer', size => 16, is_nullable => 0, is_auto_increment => 1, default_value => '', }, username=> { data_type => 'varchar', size => 64, is_nullable => 0, is_auto_increment => 0, default_value => '', }, password=> { data_type => 'varchar', size => 64, is_nullable => 0, is_auto_increment => 0, default_value => '', }, firstname=> { data_type => 'varchar', size => 64, is_nullable => 1, is_auto_increment => 0, default_value => '', }, lastname=> { data_type => 'varchar', size => 64, is_nullable => 1, is_auto_increment => 0, default_value => '', }, email=> { data_type => 'varchar', size => 128, is_nullable => 0, is_auto_increment => 0, default_value => '', }, active=> { data_type => 'integer', size => 16, is_nullable => 0, is_auto_increment => 0, default_value => '', } ); __PACKAGE__->set_primary_key('obid'); __PACKAGE__->has_many(role, 'NEOpS::WDS::Roles', role); 1;

    Roles.pm

    package NEOpS::WDS::Roles; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('roles'); __PACKAGE__->add_columns(obid=> { accessor => 'roles', data_type => 'integer', size => 16, is_nullable => 0, is_auto_increment => 1, default_value => '', }, role=> { data_type => 'varchar', size => 64, is_nullable => 0, is_auto_increment => 0, default_value => '', } ); __PACKAGE__->set_primary_key('obid'); 1;


    --Jimbus aka Jim Babcock
    Wireless Data Engineer and Geek Wannabe
    jim-dot-babcock-at-usa-dot-com
      Hi jimbus,

      Yes, my example was all in one file.. Thats generally considered the worse way to do it.. put them each in their own files if you can. (My excuse is, mine was created by the sql translator script, which can only output one file at present, and Ive been too lazy to change it ;)

      Your schema looks fairly good to me, though I think you have the arguments reversed in that has_one().. In fact, Im not sure what relationship it's describing. You're saying each system row has an corresponding entry in the users table, with the ID from the system table in it? That doesnt look like what you mean. Is Point_of_Contact a role? Can you show some example data for that please?

      C.

        You're right, I should be using a role in that place. I've had in my mind that a limited number of roles would be easiest to manage, but I don't think that's right either. But on the other hand, do I really want a role for each system/element/connection/etc on our network

        I'm also struggling from moving my mindset from normalized tables to object oriented constructs.

        What I want in the final analysis, is to have a list of systems (on a web page using Apache/Catalyst/Mason) in a tabular format with basic information displayed in the list, including the user id for the point of contact (or owner) of the system. Clicking on the system name will bring up more detailed information about the system (elements, trouble tickets, maintenace procedures and schedule, disaster recovery... I've put in those tables, but they are commented out, at the minute). Click in the PoC will bring up additional info about the owner, such as email and full name.

        I've built systems like this many times, but never in an object oriented fashion. I've always been a fan of OO and decided to try it, but I'm getting bogged down on the basic stuff... do I have to built the interrum tables, like the ones that mantain the relations between systems and roles or roles and users... when elements get added to the mix, do I add them through some interface on system or do I just add the element to the elements table update the relationship table by hand? For that matter... and I'm still reading... how do I retrieve the elements when I want to display them.

        an example of a system:

        • obid: 1<\li>
        • Name: Data Core<\li>
        • decrip: provides data flow for all traffic in the data portion of the network<\li>
        • Point_of_Contact: points to a user or role<\li>

        an example of a User:
        • obid: 42<\li>
        • username: aperson<\li>
        • password: [29134u[rmc qu&NAPEUT$peutq'tqwfj<\li>
        • firstname: Aparticular<\li>
        • lastname: Person<\li>
        • email: aperson@dobson.net<\li>


        --Jimbus aka Jim Babcock
        Wireless Data Engineer and Geek Wannabe
        jim-dot-babcock-at-usa-dot-com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://577613]
Approved by b10m
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2014-08-28 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (260 votes), past polls