Re: [Dbix-class] table-per-subclass - how it works?
Just wanted to make sure the OP had seen http://search.cpan.org/~syber/DBIx-Class-DynamicSubclass-0.03/lib/DBIx/Class/DynamicSubclass.pm It looks like once you have your subclasses working (don't ask me how), you can make sure they inflate from the DB into the correct class using __PACKAGE__->typecast_column('defining_column'); where defining_column I guess would be the primary key (of the base class table). On Mon, Jun 18, 2012 at 9:40 AM, Patrick Meidl wrote: > On Mon, Jun 18 2012, Alexander Hartmaier > wrote: > >> If I understand you correctly, you want to define a Perl object that >> doesn't directly relate to one table but to column from multiple tables? >> DBIC result objects always represent exactly one table with all its >> columns, primary and unique keys and relationships. >> What you want is a 'business' layer on top of that which you can define >> however you like, for example with Moose, that uses the DBIC objects so >> you don't have to deal with SQL. > > exactly. in java lingo: the DBIC Result object is the equivalent of a > DAO (data access object), not a business model. of course, you can (with > both Hibernate and DBIc) use your DAO as your business model, but this > is not a very clean design. > > that said, Hibernate *is* slightly more flexible regarding the mapping > of objects to tables. you can achieve similar results in DBIc with a > little bit of hand coding in the Result classes (as was pointed out by > other posters already), but it will get a bit quirky. > > in my experience, it's better to do things the way your framework > intended them, rather than trying to bend the framework to work the way > another framework does, even if you think the other design is nicer. > > HTH > > patrick > > -- > Patrick Meidl patr...@pantheon.at > Vienna, Austria .. http://gplus.to/pmeidl > > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
On Mon, Jun 18 2012, Alexander Hartmaier wrote: > If I understand you correctly, you want to define a Perl object that > doesn't directly relate to one table but to column from multiple tables? > DBIC result objects always represent exactly one table with all its > columns, primary and unique keys and relationships. > What you want is a 'business' layer on top of that which you can define > however you like, for example with Moose, that uses the DBIC objects so > you don't have to deal with SQL. exactly. in java lingo: the DBIC Result object is the equivalent of a DAO (data access object), not a business model. of course, you can (with both Hibernate and DBIc) use your DAO as your business model, but this is not a very clean design. that said, Hibernate *is* slightly more flexible regarding the mapping of objects to tables. you can achieve similar results in DBIc with a little bit of hand coding in the Result classes (as was pointed out by other posters already), but it will get a bit quirky. in my experience, it's better to do things the way your framework intended them, rather than trying to bend the framework to work the way another framework does, even if you think the other design is nicer. HTH patrick -- Patrick Meidl patr...@pantheon.at Vienna, Austria .. http://gplus.to/pmeidl ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
If I understand you correctly, you want to define a Perl object that doesn't directly relate to one table but to column from multiple tables? DBIC result objects always represent exactly one table with all its columns, primary and unique keys and relationships. What you want is a 'business' layer on top of that which you can define however you like, for example with Moose, that uses the DBIC objects so you don't have to deal with SQL. Am 2012-06-15 15:06, schrieb Alex Erzin: > Hm...You revert idea of ORM ;) > > "extend one table class (called resultsource/resultset in DBIC) to form > another table class" - I not need a _table_class_ at all. > I need just a Host object with related properties regardless of one or two or > three tables I need to represent my object in RDBMS. > In reality i need more extended hierarhy of classes, but here i try to find > elegant solution only for two objects where one is inherited from another. > And one of the ways is to use two tables where one table is represent common > fields for both classes and another table represents fields only > specific for inherited class. And when i need to create one more inherited > class i need simple to add new fields in new table - that's all. > This in theory (or in Java ;) > > In other words, is it possible in Perl do something like in Hibernate in Java > or no? > And if possible - how to do this? > > PS. I already have mentioned that have developed such subsystem some years > ago, but it was commercial project and i cannot use that solution in my > current projects. > I think that exists some free solution... seems to me i'm wrong with it. > > > > 15.06.2012, 15:53, "Alexander Hartmaier" : >> Hi Alex, >> there is no such thing as inheritance in an RDBMS, so why whould an ORM >> extend one table class (called resultsource/resultset in DBIC) to form >> another table class? >> To get you started it's usually the easiest to let dbicdump [1] create >> all the DBIC classes and look at what it generated. >> Also the glossary [2] is a great thing to read before the rest of the >> docs to know which terms the docs use for the various components. >> >> [1] https://metacpan.org/module/dbicdump >> [2] https://metacpan.org/module/DBIx::Class::Manual::Glossary >> >> BR Alex >> >> Am 2012-06-15 10:30, schrieb Alex Erzin: >> >>> I'm confused with "package Host; extends 'Base';" >>> I expect "extends 'Target'", not 'Base'. Is it correct? >>> >>> 15.06.2012, 12:06, "Hailin Hu" : I have no idea about "standard" solution, but another idea for reference :) Package Base; use base 'DBIx::Class::Core'; # parents' name should be a defined has_one or belong_to relationship has 'parents' => ( is => 'rw', isa => 'ArrayRef[Str]', builder => _build_parents, ); sub inflate_result { my $self = shift; my $ret = $self->next::method( @_ ); foreach my $relationship ( @{ $ret->parents } ) { my $parent = $ret->$relationship; my @columns = ... # @columns_in_$parent - @column_in_$ret foreach my $column ( @columns ) { # stolen from DBIx::Class's source no strict 'refs'; no warnings 'redefine'; my $method = join '::', ref $ret, $column; *$method = Sub::Name::subname( $method, sub { return $ret->$parent->$column; }); } } } ... package Target; extends 'Base'; ... package Host; extends 'Base'; __PACKAGE__->belong_to('target' => 'Target', 'target_id'); sub _build_parents {[qw/target/]} ... so just declare the attribute 'parents' in subclass is ok. the task is not typical for me, but interesting. On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: > Yes, in some cases you are right, but you are saying about "long > inheritance chains" and other cases where inheritance is not enought > flexible, > but i say about just one case in one place where inheritance is > preferred for me, and i try to find solution _how_ to implement > inheritance, but not about _why_not_ :) > > 15.06.2012, 10:43, "Ben Tilly" : >> The benefits of OOP are mostly in information hiding. >> >> If you're creating a bunch of long inheritance chains, you're probably >> doing OOP poorly. Composition is preferred. And maps *much* more >> naturally onto a relational database. >> >> On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin >> wrote: >>>Hm... it really works, but where are benefits of OOP? ;) >>>There is no inheritance at all, and to access parent's properties I >>> should call parent directly, so inheritance just replaced with >>> incapsulation, and it's no one what i try to find. >>> >>>Some years ago we have implemented inheritance in ORM in
Re: [Dbix-class] table-per-subclass - how it works?
On 15/06/12 07:04, Alex Erzin wrote: Hello, [ ... ] CREATE TABLE `mydb`.`target` ( `target_id` INT NOT NULL , `description` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) ) CREATE TABLE `mydb`.`host` ( `target_id` INT NOT NULL , `hostname` VARCHAR(45) NULL , `ip` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) , CONSTRAINT `fk_host_target` FOREIGN KEY (`target_id` ) REFERENCES `mydb`.`target` (`target_id` ) ) How to implemeng there classes Target and Host, so it possible to write print $target->id(), $targer->description() print $host->id(), $host->description(), $host->hostname(), $host->ip() ? I have no ideas how to implement inheritance, and all my experiments are failed - from Host i cannot get access to parent properties like description. Could you help me please (with examples)? I don't know how you'd get $host->id (maybe you meant $host->target_id?), but you can proxy accessors across to other table definitions by specifying them in your relationship definition (see the 'proxy' section under https://metacpan.org/module/DBIx::Class::ResultSource#add_relationship). Darius NET-A-PORTER.COM Irresistible fashion at your fingertips CONFIDENTIALITY NOTICE The information in this email is confidential and is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, you must not read, use or disseminate the information. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Net-A-Porter Group Limited. The Net-A-Porter Group Limited is a company registered in England & Wales Number: 3820604 Registered Office: 1 The Village Offices, Westfield, Ariel Way, London, W12 7GF ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
Hm...You revert idea of ORM ;) "extend one table class (called resultsource/resultset in DBIC) to form another table class" - I not need a _table_class_ at all. I need just a Host object with related properties regardless of one or two or three tables I need to represent my object in RDBMS. In reality i need more extended hierarhy of classes, but here i try to find elegant solution only for two objects where one is inherited from another. And one of the ways is to use two tables where one table is represent common fields for both classes and another table represents fields only specific for inherited class. And when i need to create one more inherited class i need simple to add new fields in new table - that's all. This in theory (or in Java ;) In other words, is it possible in Perl do something like in Hibernate in Java or no? And if possible - how to do this? PS. I already have mentioned that have developed such subsystem some years ago, but it was commercial project and i cannot use that solution in my current projects. I think that exists some free solution... seems to me i'm wrong with it. 15.06.2012, 15:53, "Alexander Hartmaier" : > Hi Alex, > there is no such thing as inheritance in an RDBMS, so why whould an ORM > extend one table class (called resultsource/resultset in DBIC) to form > another table class? > To get you started it's usually the easiest to let dbicdump [1] create > all the DBIC classes and look at what it generated. > Also the glossary [2] is a great thing to read before the rest of the > docs to know which terms the docs use for the various components. > > [1] https://metacpan.org/module/dbicdump > [2] https://metacpan.org/module/DBIx::Class::Manual::Glossary > > BR Alex > > Am 2012-06-15 10:30, schrieb Alex Erzin: > >> I'm confused with "package Host; extends 'Base';" >> I expect "extends 'Target'", not 'Base'. Is it correct? >> >> 15.06.2012, 12:06, "Hailin Hu" : >>> I have no idea about "standard" solution, but another idea for reference :) >>> >>> Package Base; >>> use base 'DBIx::Class::Core'; >>> >>> # parents' name should be a defined has_one or belong_to relationship >>> has 'parents' => ( >>> is => 'rw', >>> isa => 'ArrayRef[Str]', >>> builder => _build_parents, >>> ); >>> >>> sub inflate_result { >>> my $self = shift; >>> my $ret = $self->next::method( @_ ); >>> foreach my $relationship ( @{ $ret->parents } ) { >>> my $parent = $ret->$relationship; >>> my @columns = ... # @columns_in_$parent - @column_in_$ret >>> foreach my $column ( @columns ) { >>> # stolen from DBIx::Class's source >>> no strict 'refs'; >>> no warnings 'redefine'; >>> my $method = join '::', ref $ret, $column; >>> *$method = Sub::Name::subname( $method, sub { >>> return $ret->$parent->$column; >>> }); >>> } >>> } >>> } >>> ... >>> >>> package Target; >>> extends 'Base'; >>> ... >>> >>> package Host; >>> extends 'Base'; >>> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >>> sub _build_parents {[qw/target/]} >>> ... >>> >>> so just declare the attribute 'parents' in subclass is ok. >>> the task is not typical for me, but interesting. >>> >>> On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: Yes, in some cases you are right, but you are saying about "long inheritance chains" and other cases where inheritance is not enought flexible, but i say about just one case in one place where inheritance is preferred for me, and i try to find solution _how_ to implement inheritance, but not about _why_not_ :) 15.06.2012, 10:43, "Ben Tilly" : > The benefits of OOP are mostly in information hiding. > > If you're creating a bunch of long inheritance chains, you're probably > doing OOP poorly. Composition is preferred. And maps *much* more > naturally onto a relational database. > > On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: >> Hm... it really works, but where are benefits of OOP? ;) >> There is no inheritance at all, and to access parent's properties I >> should call parent directly, so inheritance just replaced with >> incapsulation, and it's no one what i try to find. >> >> Some years ago we have implemented inheritance in ORM in some >> commercial project in Perl, later I have swithed to java and have found >> Hibernate it really cool with ORM. >> But now I have returned to Perl and I think that DBIx::Class can help >> me with ORM, but right now i'm confused that cannot find "standard" >> solution for typical task. >> >> 15.06.2012, 10:25, "Hailin Hu" : >>> Is it acceptable like something below >>> >>> package Target; >>> ... >>> package Host; >>> ... >>> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >>> sub column_only_exists_in_target { >>
Re: [Dbix-class] table-per-subclass - how it works?
Hi Alex, there is no such thing as inheritance in an RDBMS, so why whould an ORM extend one table class (called resultsource/resultset in DBIC) to form another table class? To get you started it's usually the easiest to let dbicdump [1] create all the DBIC classes and look at what it generated. Also the glossary [2] is a great thing to read before the rest of the docs to know which terms the docs use for the various components. [1] https://metacpan.org/module/dbicdump [2] https://metacpan.org/module/DBIx::Class::Manual::Glossary BR Alex Am 2012-06-15 10:30, schrieb Alex Erzin: > I'm confused with "package Host; extends 'Base';" > I expect "extends 'Target'", not 'Base'. Is it correct? > > 15.06.2012, 12:06, "Hailin Hu" : >> I have no idea about "standard" solution, but another idea for reference :) >> >> Package Base; >> use base 'DBIx::Class::Core'; >> >> # parents' name should be a defined has_one or belong_to relationship >> has 'parents' => ( >> is => 'rw', >> isa => 'ArrayRef[Str]', >> builder => _build_parents, >> ); >> >> sub inflate_result { >> my $self = shift; >> my $ret = $self->next::method( @_ ); >> foreach my $relationship ( @{ $ret->parents } ) { >> my $parent = $ret->$relationship; >> my @columns = ... # @columns_in_$parent - @column_in_$ret >> foreach my $column ( @columns ) { >> # stolen from DBIx::Class's source >> no strict 'refs'; >> no warnings 'redefine'; >> my $method = join '::', ref $ret, $column; >> *$method = Sub::Name::subname( $method, sub { >> return $ret->$parent->$column; >> }); >> } >> } >> } >> ... >> >> package Target; >> extends 'Base'; >> ... >> >> package Host; >> extends 'Base'; >> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >> sub _build_parents {[qw/target/]} >> ... >> >> so just declare the attribute 'parents' in subclass is ok. >> the task is not typical for me, but interesting. >> >> On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: >> >>> Yes, in some cases you are right, but you are saying about "long >>> inheritance chains" and other cases where inheritance is not enought >>> flexible, >>> but i say about just one case in one place where inheritance is preferred >>> for me, and i try to find solution _how_ to implement inheritance, but not >>> about _why_not_ :) >>> >>> 15.06.2012, 10:43, "Ben Tilly" : The benefits of OOP are mostly in information hiding. If you're creating a bunch of long inheritance chains, you're probably doing OOP poorly. Composition is preferred. And maps *much* more naturally onto a relational database. On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: > Hm... it really works, but where are benefits of OOP? ;) > There is no inheritance at all, and to access parent's properties I > should call parent directly, so inheritance just replaced with > incapsulation, and it's no one what i try to find. > > Some years ago we have implemented inheritance in ORM in some > commercial project in Perl, later I have swithed to java and have found > Hibernate it really cool with ORM. > But now I have returned to Perl and I think that DBIx::Class can help > me with ORM, but right now i'm confused that cannot find "standard" > solution for typical task. > > 15.06.2012, 10:25, "Hailin Hu" : >> Is it acceptable like something below >> >> package Target; >> ... >> package Host; >> ... >> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >> sub column_only_exists_in_target { >> my $self = shift; >> return $self->target->column_only_exists_in_target; >> } >> >> well, it is not a good solution, but it could work, I think :) >> >> On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: >>>Hello, >>> >>>I need to implement table-per-class inheritance, as it simple made >>> in Hibernate in Java (more details can be found here >>> http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), >>>but cannot find right way how to do it in DBIx::Class. >>> >>>For example, I have two objects: Target (with properties ID, >>> Description) and Host (ID, Hostname, IP), Host inherited from Target, >>> and on table-level there are two tables Host and Target, joined by ID >>> field. >>> >>>CREATE TABLE `mydb`.`target` ( >>> `target_id` INT NOT NULL , >>> `description` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) ) >>> >>>CREATE TABLE `mydb`.`host` ( >>> `target_id` INT NOT NULL , >>> `hostname` VARCHAR(45) NULL , >>> `ip` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) , >>> CONSTRAINT `fk_host_target` >>> FOREIGN KEY (`target_id` ) >>> REFERENCES `mydb`.`targe
Re: [Dbix-class] table-per-subclass - how it works?
No, on subclass layer, Host has no relationship with Target. The idea is a little tricky. Inheritage is implemented according to the attribute 'parents' instead of the natural class inheritage. In class 'Host' >> sub _build_parents {[qw/target/]} This declaresd Host's parent as target( the relationship method name ). Since 'Host' extends 'Base' >> sub inflate_result { This would be called when object is inited, which dynamically creates so-called inherited accessors. On Fri, Jun 15, 2012 at 5:30 PM, Alex Erzin wrote: > I'm confused with "package Host; extends 'Base';" > I expect "extends 'Target'", not 'Base'. Is it correct? > > 15.06.2012, 12:06, "Hailin Hu" : >> I have no idea about "standard" solution, but another idea for reference :) >> >> Package Base; >> use base 'DBIx::Class::Core'; >> >> # parents' name should be a defined has_one or belong_to relationship >> has 'parents' => ( >> is => 'rw', >> isa => 'ArrayRef[Str]', >> builder => _build_parents, >> ); >> >> sub inflate_result { >> my $self = shift; >> my $ret = $self->next::method( @_ ); >> foreach my $relationship ( @{ $ret->parents } ) { >> my $parent = $ret->$relationship; >> my @columns = ... # @columns_in_$parent - @column_in_$ret >> foreach my $column ( @columns ) { >> # stolen from DBIx::Class's source >> no strict 'refs'; >> no warnings 'redefine'; >> my $method = join '::', ref $ret, $column; >> *$method = Sub::Name::subname( $method, sub { >> return $ret->$parent->$column; >> }); >> } >> } >> } >> ... >> >> package Target; >> extends 'Base'; >> ... >> >> package Host; >> extends 'Base'; >> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >> sub _build_parents {[qw/target/]} >> ... >> >> so just declare the attribute 'parents' in subclass is ok. >> the task is not typical for me, but interesting. >> >> On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: >> >>> Yes, in some cases you are right, but you are saying about "long >>> inheritance chains" and other cases where inheritance is not enought >>> flexible, >>> but i say about just one case in one place where inheritance is preferred >>> for me, and i try to find solution _how_ to implement inheritance, but not >>> about _why_not_ :) >>> >>> 15.06.2012, 10:43, "Ben Tilly" : The benefits of OOP are mostly in information hiding. If you're creating a bunch of long inheritance chains, you're probably doing OOP poorly. Composition is preferred. And maps *much* more naturally onto a relational database. On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: > Hm... it really works, but where are benefits of OOP? ;) > There is no inheritance at all, and to access parent's properties I > should call parent directly, so inheritance just replaced with > incapsulation, and it's no one what i try to find. > > Some years ago we have implemented inheritance in ORM in some > commercial project in Perl, later I have swithed to java and have found > Hibernate it really cool with ORM. > But now I have returned to Perl and I think that DBIx::Class can help > me with ORM, but right now i'm confused that cannot find "standard" > solution for typical task. > > 15.06.2012, 10:25, "Hailin Hu" : >> Is it acceptable like something below >> >> package Target; >> ... >> package Host; >> ... >> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >> sub column_only_exists_in_target { >> my $self = shift; >> return $self->target->column_only_exists_in_target; >> } >> >> well, it is not a good solution, but it could work, I think :) >> >> On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: >>> Hello, >>> >>> I need to implement table-per-class inheritance, as it simple made >>> in Hibernate in Java (more details can be found here >>> http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), >>> but cannot find right way how to do it in DBIx::Class. >>> >>> For example, I have two objects: Target (with properties ID, >>> Description) and Host (ID, Hostname, IP), Host inherited from Target, >>> and on table-level there are two tables Host and Target, joined by ID >>> field. >>> >>> CREATE TABLE `mydb`.`target` ( >>> `target_id` INT NOT NULL , >>> `description` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) ) >>> >>> CREATE TABLE `mydb`.`host` ( >>> `target_id` INT NOT NULL , >>> `hostname` VARCHAR(45) NULL , >>> `ip` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) , >>> CONSTRAINT `fk_host_target` >>> FOREIGN KEY (`target_id` ) >>> REFERENCES `mydb`.`target` (`target_id` ) >>> ) >>> >>> How to
Re: [Dbix-class] table-per-subclass - how it works?
I'm confused with "package Host; extends 'Base';" I expect "extends 'Target'", not 'Base'. Is it correct? 15.06.2012, 12:06, "Hailin Hu" : > I have no idea about "standard" solution, but another idea for reference :) > > Package Base; > use base 'DBIx::Class::Core'; > > # parents' name should be a defined has_one or belong_to relationship > has 'parents' => ( > is => 'rw', > isa => 'ArrayRef[Str]', > builder => _build_parents, > ); > > sub inflate_result { > my $self = shift; > my $ret = $self->next::method( @_ ); > foreach my $relationship ( @{ $ret->parents } ) { > my $parent = $ret->$relationship; > my @columns = ... # @columns_in_$parent - @column_in_$ret > foreach my $column ( @columns ) { > # stolen from DBIx::Class's source > no strict 'refs'; > no warnings 'redefine'; > my $method = join '::', ref $ret, $column; > *$method = Sub::Name::subname( $method, sub { > return $ret->$parent->$column; > }); > } > } > } > ... > > package Target; > extends 'Base'; > ... > > package Host; > extends 'Base'; > __PACKAGE__->belong_to('target' => 'Target', 'target_id'); > sub _build_parents {[qw/target/]} > ... > > so just declare the attribute 'parents' in subclass is ok. > the task is not typical for me, but interesting. > > On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: > >> Yes, in some cases you are right, but you are saying about "long >> inheritance chains" and other cases where inheritance is not enought >> flexible, >> but i say about just one case in one place where inheritance is preferred >> for me, and i try to find solution _how_ to implement inheritance, but not >> about _why_not_ :) >> >> 15.06.2012, 10:43, "Ben Tilly" : >>> The benefits of OOP are mostly in information hiding. >>> >>> If you're creating a bunch of long inheritance chains, you're probably >>> doing OOP poorly. Composition is preferred. And maps *much* more >>> naturally onto a relational database. >>> >>> On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: Hm... it really works, but where are benefits of OOP? ;) There is no inheritance at all, and to access parent's properties I should call parent directly, so inheritance just replaced with incapsulation, and it's no one what i try to find. Some years ago we have implemented inheritance in ORM in some commercial project in Perl, later I have swithed to java and have found Hibernate it really cool with ORM. But now I have returned to Perl and I think that DBIx::Class can help me with ORM, but right now i'm confused that cannot find "standard" solution for typical task. 15.06.2012, 10:25, "Hailin Hu" : > Is it acceptable like something below > > package Target; > ... > package Host; > ... > __PACKAGE__->belong_to('target' => 'Target', 'target_id'); > sub column_only_exists_in_target { > my $self = shift; > return $self->target->column_only_exists_in_target; > } > > well, it is not a good solution, but it could work, I think :) > > On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: >> Hello, >> >> I need to implement table-per-class inheritance, as it simple made in >> Hibernate in Java (more details can be found here >> http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), >> but cannot find right way how to do it in DBIx::Class. >> >> For example, I have two objects: Target (with properties ID, >> Description) and Host (ID, Hostname, IP), Host inherited from Target, >> and on table-level there are two tables Host and Target, joined by ID >> field. >> >> CREATE TABLE `mydb`.`target` ( >> `target_id` INT NOT NULL , >> `description` VARCHAR(45) NULL , >> PRIMARY KEY (`target_id`) ) >> >> CREATE TABLE `mydb`.`host` ( >> `target_id` INT NOT NULL , >> `hostname` VARCHAR(45) NULL , >> `ip` VARCHAR(45) NULL , >> PRIMARY KEY (`target_id`) , >> CONSTRAINT `fk_host_target` >> FOREIGN KEY (`target_id` ) >> REFERENCES `mydb`.`target` (`target_id` ) >> ) >> >> How to implemeng there classes Target and Host, so it possible to >> write >> >> print $target->id(), $targer->description() >> print $host->id(), $host->description(), $host->hostname(), >> $host->ip() >> >> ? >> >> I have no ideas how to implement inheritance, and all my experiments >> are failed - from Host i cannot get access to parent properties like >> description. >> Could you help me please (with examples)? >> >> Thanks. >> >> -- >> eaa@ >> >> ___ >> List: http://lists.scsys.co.uk/cgi-bin/m
Re: [Dbix-class] table-per-subclass - how it works?
I have no idea about "standard" solution, but another idea for reference :) Package Base; use base 'DBIx::Class::Core'; # parents' name should be a defined has_one or belong_to relationship has 'parents' => ( is => 'rw', isa => 'ArrayRef[Str]', builder => _build_parents, ); sub inflate_result { my $self = shift; my $ret = $self->next::method( @_ ); foreach my $relationship ( @{ $ret->parents } ) { my $parent = $ret->$relationship; my @columns = ... # @columns_in_$parent - @column_in_$ret foreach my $column ( @columns ) { # stolen from DBIx::Class's source no strict 'refs'; no warnings 'redefine'; my $method = join '::', ref $ret, $column; *$method = Sub::Name::subname( $method, sub { return $ret->$parent->$column; }); } } } ... package Target; extends 'Base'; ... package Host; extends 'Base'; __PACKAGE__->belong_to('target' => 'Target', 'target_id'); sub _build_parents {[qw/target/]} ... so just declare the attribute 'parents' in subclass is ok. the task is not typical for me, but interesting. On Fri, Jun 15, 2012 at 3:59 PM, Alex Erzin wrote: > Yes, in some cases you are right, but you are saying about "long inheritance > chains" and other cases where inheritance is not enought flexible, > but i say about just one case in one place where inheritance is preferred for > me, and i try to find solution _how_ to implement inheritance, but not about > _why_not_ :) > > > 15.06.2012, 10:43, "Ben Tilly" : >> The benefits of OOP are mostly in information hiding. >> >> If you're creating a bunch of long inheritance chains, you're probably >> doing OOP poorly. Composition is preferred. And maps *much* more >> naturally onto a relational database. >> >> On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: >> >>> Hm... it really works, but where are benefits of OOP? ;) >>> There is no inheritance at all, and to access parent's properties I should >>> call parent directly, so inheritance just replaced with incapsulation, and >>> it's no one what i try to find. >>> >>> Some years ago we have implemented inheritance in ORM in some commercial >>> project in Perl, later I have swithed to java and have found Hibernate it >>> really cool with ORM. >>> But now I have returned to Perl and I think that DBIx::Class can help me >>> with ORM, but right now i'm confused that cannot find "standard" solution >>> for typical task. >>> >>> 15.06.2012, 10:25, "Hailin Hu" : Is it acceptable like something below package Target; ... package Host; ... __PACKAGE__->belong_to('target' => 'Target', 'target_id'); sub column_only_exists_in_target { my $self = shift; return $self->target->column_only_exists_in_target; } well, it is not a good solution, but it could work, I think :) On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: > Hello, > > I need to implement table-per-class inheritance, as it simple made in > Hibernate in Java (more details can be found here > http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), > but cannot find right way how to do it in DBIx::Class. > > For example, I have two objects: Target (with properties ID, > Description) and Host (ID, Hostname, IP), Host inherited from Target, and > on table-level there are two tables Host and Target, joined by ID field. > > CREATE TABLE `mydb`.`target` ( > `target_id` INT NOT NULL , > `description` VARCHAR(45) NULL , > PRIMARY KEY (`target_id`) ) > > CREATE TABLE `mydb`.`host` ( > `target_id` INT NOT NULL , > `hostname` VARCHAR(45) NULL , > `ip` VARCHAR(45) NULL , > PRIMARY KEY (`target_id`) , > CONSTRAINT `fk_host_target` > FOREIGN KEY (`target_id` ) > REFERENCES `mydb`.`target` (`target_id` ) > ) > > How to implemeng there classes Target and Host, so it possible to write > > print $target->id(), $targer->description() > print $host->id(), $host->description(), $host->hostname(), $host->ip() > > ? > > I have no ideas how to implement inheritance, and all my experiments > are failed - from Host i cannot get access to parent properties like > description. > Could you help me please (with examples)? > > Thanks. > > -- > eaa@ > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: > http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class >>>
Re: [Dbix-class] table-per-subclass - how it works?
Yes, in some cases you are right, but you are saying about "long inheritance chains" and other cases where inheritance is not enought flexible, but i say about just one case in one place where inheritance is preferred for me, and i try to find solution _how_ to implement inheritance, but not about _why_not_ :) 15.06.2012, 10:43, "Ben Tilly" : > The benefits of OOP are mostly in information hiding. > > If you're creating a bunch of long inheritance chains, you're probably > doing OOP poorly. Composition is preferred. And maps *much* more > naturally onto a relational database. > > On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: > >> Hm... it really works, but where are benefits of OOP? ;) >> There is no inheritance at all, and to access parent's properties I should >> call parent directly, so inheritance just replaced with incapsulation, and >> it's no one what i try to find. >> >> Some years ago we have implemented inheritance in ORM in some commercial >> project in Perl, later I have swithed to java and have found Hibernate it >> really cool with ORM. >> But now I have returned to Perl and I think that DBIx::Class can help me >> with ORM, but right now i'm confused that cannot find "standard" solution >> for typical task. >> >> 15.06.2012, 10:25, "Hailin Hu" : >>> Is it acceptable like something below >>> >>> package Target; >>> ... >>> package Host; >>> ... >>> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >>> sub column_only_exists_in_target { >>> my $self = shift; >>> return $self->target->column_only_exists_in_target; >>> } >>> >>> well, it is not a good solution, but it could work, I think :) >>> >>> On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: Hello, I need to implement table-per-class inheritance, as it simple made in Hibernate in Java (more details can be found here http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), but cannot find right way how to do it in DBIx::Class. For example, I have two objects: Target (with properties ID, Description) and Host (ID, Hostname, IP), Host inherited from Target, and on table-level there are two tables Host and Target, joined by ID field. CREATE TABLE `mydb`.`target` ( `target_id` INT NOT NULL , `description` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) ) CREATE TABLE `mydb`.`host` ( `target_id` INT NOT NULL , `hostname` VARCHAR(45) NULL , `ip` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) , CONSTRAINT `fk_host_target` FOREIGN KEY (`target_id` ) REFERENCES `mydb`.`target` (`target_id` ) ) How to implemeng there classes Target and Host, so it possible to write print $target->id(), $targer->description() print $host->id(), $host->description(), $host->hostname(), $host->ip() ? I have no ideas how to implement inheritance, and all my experiments are failed - from Host i cannot get access to parent properties like description. Could you help me please (with examples)? Thanks. -- eaa@ ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk >>> ___ >>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class >>> IRC: irc.perl.org#dbix-class >>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ >>> Searchable Archive: >>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk >> -- >> eaa@ >> >> ___ >> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class >> IRC: irc.perl.org#dbix-class >> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ >> Searchable Archive: >> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk -- eaa@ ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
The benefits of OOP are mostly in information hiding. If you're creating a bunch of long inheritance chains, you're probably doing OOP poorly. Composition is preferred. And maps *much* more naturally onto a relational database. On Thu, Jun 14, 2012 at 11:37 PM, Alex Erzin wrote: > Hm... it really works, but where are benefits of OOP? ;) > There is no inheritance at all, and to access parent's properties I should > call parent directly, so inheritance just replaced with incapsulation, and > it's no one what i try to find. > > Some years ago we have implemented inheritance in ORM in some commercial > project in Perl, later I have swithed to java and have found Hibernate it > really cool with ORM. > But now I have returned to Perl and I think that DBIx::Class can help me with > ORM, but right now i'm confused that cannot find "standard" solution for > typical task. > > > 15.06.2012, 10:25, "Hailin Hu" : >> Is it acceptable like something below >> >> package Target; >> ... >> package Host; >> ... >> __PACKAGE__->belong_to('target' => 'Target', 'target_id'); >> sub column_only_exists_in_target { >> my $self = shift; >> return $self->target->column_only_exists_in_target; >> } >> >> well, it is not a good solution, but it could work, I think :) >> >> On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: >> >>> Hello, >>> >>> I need to implement table-per-class inheritance, as it simple made in >>> Hibernate in Java (more details can be found here >>> http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), >>> but cannot find right way how to do it in DBIx::Class. >>> >>> For example, I have two objects: Target (with properties ID, Description) >>> and Host (ID, Hostname, IP), Host inherited from Target, and on table-level >>> there are two tables Host and Target, joined by ID field. >>> >>> CREATE TABLE `mydb`.`target` ( >>> `target_id` INT NOT NULL , >>> `description` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) ) >>> >>> CREATE TABLE `mydb`.`host` ( >>> `target_id` INT NOT NULL , >>> `hostname` VARCHAR(45) NULL , >>> `ip` VARCHAR(45) NULL , >>> PRIMARY KEY (`target_id`) , >>> CONSTRAINT `fk_host_target` >>> FOREIGN KEY (`target_id` ) >>> REFERENCES `mydb`.`target` (`target_id` ) >>> ) >>> >>> How to implemeng there classes Target and Host, so it possible to write >>> >>> print $target->id(), $targer->description() >>> print $host->id(), $host->description(), $host->hostname(), $host->ip() >>> >>> ? >>> >>> I have no ideas how to implement inheritance, and all my experiments are >>> failed - from Host i cannot get access to parent properties like >>> description. >>> Could you help me please (with examples)? >>> >>> Thanks. >>> >>> -- >>> eaa@ >>> >>> ___ >>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class >>> IRC: irc.perl.org#dbix-class >>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ >>> Searchable Archive: >>> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk >> >> ___ >> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class >> IRC: irc.perl.org#dbix-class >> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ >> Searchable Archive: >> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk > > -- > eaa@ > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
Hm... it really works, but where are benefits of OOP? ;) There is no inheritance at all, and to access parent's properties I should call parent directly, so inheritance just replaced with incapsulation, and it's no one what i try to find. Some years ago we have implemented inheritance in ORM in some commercial project in Perl, later I have swithed to java and have found Hibernate it really cool with ORM. But now I have returned to Perl and I think that DBIx::Class can help me with ORM, but right now i'm confused that cannot find "standard" solution for typical task. 15.06.2012, 10:25, "Hailin Hu" : > Is it acceptable like something below > > package Target; > ... > package Host; > ... > __PACKAGE__->belong_to('target' => 'Target', 'target_id'); > sub column_only_exists_in_target { > my $self = shift; > return $self->target->column_only_exists_in_target; > } > > well, it is not a good solution, but it could work, I think :) > > On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: > >> Hello, >> >> I need to implement table-per-class inheritance, as it simple made in >> Hibernate in Java (more details can be found here >> http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), >> but cannot find right way how to do it in DBIx::Class. >> >> For example, I have two objects: Target (with properties ID, Description) >> and Host (ID, Hostname, IP), Host inherited from Target, and on table-level >> there are two tables Host and Target, joined by ID field. >> >> CREATE TABLE `mydb`.`target` ( >> `target_id` INT NOT NULL , >> `description` VARCHAR(45) NULL , >> PRIMARY KEY (`target_id`) ) >> >> CREATE TABLE `mydb`.`host` ( >> `target_id` INT NOT NULL , >> `hostname` VARCHAR(45) NULL , >> `ip` VARCHAR(45) NULL , >> PRIMARY KEY (`target_id`) , >> CONSTRAINT `fk_host_target` >> FOREIGN KEY (`target_id` ) >> REFERENCES `mydb`.`target` (`target_id` ) >> ) >> >> How to implemeng there classes Target and Host, so it possible to write >> >> print $target->id(), $targer->description() >> print $host->id(), $host->description(), $host->hostname(), $host->ip() >> >> ? >> >> I have no ideas how to implement inheritance, and all my experiments are >> failed - from Host i cannot get access to parent properties like description. >> Could you help me please (with examples)? >> >> Thanks. >> >> -- >> eaa@ >> >> ___ >> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class >> IRC: irc.perl.org#dbix-class >> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ >> Searchable Archive: >> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk -- eaa@ ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] table-per-subclass - how it works?
Is it acceptable like something below package Target; ... package Host; ... __PACKAGE__->belong_to('target' => 'Target', 'target_id'); sub column_only_exists_in_target { my $self = shift; return $self->target->column_only_exists_in_target; } well, it is not a good solution, but it could work, I think :) On Fri, Jun 15, 2012 at 3:04 PM, Alex Erzin wrote: > Hello, > > I need to implement table-per-class inheritance, as it simple made in > Hibernate in Java (more details can be found here > http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), > but cannot find right way how to do it in DBIx::Class. > > For example, I have two objects: Target (with properties ID, Description) and > Host (ID, Hostname, IP), Host inherited from Target, and on table-level there > are two tables Host and Target, joined by ID field. > > CREATE TABLE `mydb`.`target` ( > `target_id` INT NOT NULL , > `description` VARCHAR(45) NULL , > PRIMARY KEY (`target_id`) ) > > CREATE TABLE `mydb`.`host` ( > `target_id` INT NOT NULL , > `hostname` VARCHAR(45) NULL , > `ip` VARCHAR(45) NULL , > PRIMARY KEY (`target_id`) , > CONSTRAINT `fk_host_target` > FOREIGN KEY (`target_id` ) > REFERENCES `mydb`.`target` (`target_id` ) > ) > > How to implemeng there classes Target and Host, so it possible to write > > print $target->id(), $targer->description() > print $host->id(), $host->description(), $host->hostname(), $host->ip() > > ? > > I have no ideas how to implement inheritance, and all my experiments are > failed - from Host i cannot get access to parent properties like description. > Could you help me please (with examples)? > > Thanks. > > -- > eaa@ > > ___ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
[Dbix-class] table-per-subclass - how it works?
Hello, I need to implement table-per-class inheritance, as it simple made in Hibernate in Java (more details can be found here http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch10.html#inheritance-tablepersubclass), but cannot find right way how to do it in DBIx::Class. For example, I have two objects: Target (with properties ID, Description) and Host (ID, Hostname, IP), Host inherited from Target, and on table-level there are two tables Host and Target, joined by ID field. CREATE TABLE `mydb`.`target` ( `target_id` INT NOT NULL , `description` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) ) CREATE TABLE `mydb`.`host` ( `target_id` INT NOT NULL , `hostname` VARCHAR(45) NULL , `ip` VARCHAR(45) NULL , PRIMARY KEY (`target_id`) , CONSTRAINT `fk_host_target` FOREIGN KEY (`target_id` ) REFERENCES `mydb`.`target` (`target_id` ) ) How to implemeng there classes Target and Host, so it possible to write print $target->id(), $targer->description() print $host->id(), $host->description(), $host->hostname(), $host->ip() ? I have no ideas how to implement inheritance, and all my experiments are failed - from Host i cannot get access to parent properties like description. Could you help me please (with examples)? Thanks. -- eaa@ ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk