FAQ
I have two databases that are very similar. For example, the "person" table
in one db has an "email" column and the other does not have that column,
otherwise the tables are the same.

My application uses the schema with the table that has the "email" column,
so there's places in the application that call $person->get_column( 'email'
), and also $person_rs->create( { name => $name, email => $email } );

Now, I want to use the second schema with the same application, but as
mentioned above does not have an "email" column on the person table. it's
not important that the column does not exist.

My question is what can I do to make get_column and create (and other
methods that assume there is an "email" column) work without this column
defined in the result class. Is there any way to have define a column that
is never used when constructing database queries?


I can create a method in the result class of the schema that doesn't have
that column:

sub email { return 'dummy@example.com' }


And that will handle $person->email and even $person->email( $new_email ),
although I don't see that ever being used.

I'm not sure how to best override get_column(), though. I suppose the best
approach would be to replace all the get_column() calls in the application
with actual method calls (i.e. ->email). Or is there a way to override
get_column in this case?

What about the create() method? Use an "around create => sub {}" in the
ResultClass and strip out the "email" column?




--
Bill Moseley
moseley@hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110516/aaa83448/attachment.htm

Search Discussions

  • Octavian Rasnita at May 17, 2011 at 5:31 am
    From: Bill Moseley




    I have two databases that are very similar. For example, the "person" table in one db has an "email" column and the other does not have that column, otherwise the tables are the same.


    My application uses the schema with the table that has the "email" column, so there's places in the application that call $person->get_column( 'email' ), and also $person_rs->create( { name => $name, email => $email } );


    Now, I want to use the second schema with the same application, but as mentioned above does not have an "email" column on the person table. it's not important that the column does not exist.


    My question is what can I do to make get_column and create (and other methods that assume there is an "email" column) work without this column defined in the result class. Is there any way to have define a column that is never used when constructing database queries?




    Hi,

    Use in the Result class:

    __PACKAGE__->mk_group_accessors( simple => 'email' );

    Octavian
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110517/2c9e6cbc/attachment.htm
  • Bill Moseley at May 17, 2011 at 2:22 pm

    On Mon, May 16, 2011 at 10:31 PM, Octavian Rasnita wrote:

    *From:* Bill Moseley <moseley@hank.org>



    I have two databases that are very similar. For example, the "person"
    table in one db has an "email" column and the other does not have that
    column, otherwise the tables are the same.

    My application uses the schema with the table that has the "email" column,
    so there's places in the application that call $person->get_column( 'email'
    ), and also $person_rs->create( { name => $name, email => $email } );

    Now, I want to use the second schema with the same application, but as
    mentioned above does not have an "email" column on the person table. it's
    not important that the column does not exist.

    My question is what can I do to make get_column and create (and other
    methods that assume there is an "email" column) work without this column
    defined in the result class. Is there any way to have define a column that
    is never used when constructing database queries?



    Hi,

    Use in the Result class:

    __PACKAGE__->mk_group_accessors( simple => 'email' );

    No. Although that makes an accessor (just as my "sub email {}" example
    would provide) it doesn't register a column so ->get_column and ->create
    will still fail.



    --
    Bill Moseley
    moseley@hank.org
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110517/ac71a811/attachment.htm
  • Peter Edwards at May 17, 2011 at 2:43 pm
    On 17 May 2011 15:22, Bill Moseley wrote:
    I have two databases that are very similar. For example, the "person"
    table in one db has an "email" column and the other does not have that
    column, otherwise the tables are the same.
    ...

    My question is what can I do to make get_column and create (and other
    methods that assume there is an "email" column) work without this column
    defined in the result class. Is there any way to have define a column that
    is never used when constructing database queries?

    Can you say

    my $search_attr = {
    '+columns' => [ { 'email' => \'' } ], # and so on for other missing
    fields
    };

    then

    my $rs = $schema->search_rs( $query, $search_attr );


    Regards, Peter
    http://perl.dragonstaff.co.uk
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110517/8550a736/attachment.htm
  • Bill Moseley at May 17, 2011 at 3:14 pm

    On Tue, May 17, 2011 at 7:43 AM, Peter Edwards wrote:

    Can you say

    my $search_attr = {
    '+columns' => [ { 'email' => \'' } ], # and so on for other missing
    fields
    };

    then

    my $rs = $schema->search_rs( $query, $search_attr );
    Hi Peter,

    Maybe I'm not understanding your suggestion. The problem I have is an
    existing application that uses DBIC, and (perhaps incorrectly) there's quite
    a few direct uses of $user->get_column( 'email' ), I could probably replace
    those in the app by adding, for example, an "email" method to the result
    class. There's also other uses of the "email" column such as create() and
    maybe other resultset methods that verify that every key in the passed in
    data is an actual column.

    I suspect replacing the get_column() usage is necessary. But, I'm not sure
    how best to deal with any other methods that might reference a non-existent
    column.

    Really, what I need to do is prevent the column from ending up in SQL. So,
    could I defined the non-existent column in the result class and then
    override the SQL generation somewhere to strip out the columns? I suspect
    that's less that trivial.

    Other option would be to override insert and create and strip out the
    non-existent columns.

    Unfortunately, I can't just add the columns to the database.

    Any other ideas?

    Thanks,



    --
    Bill Moseley
    moseley@hank.org
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110517/059e7f8b/attachment.htm
  • Alexander Hartmaier at May 18, 2011 at 9:11 am
    Why not create another result class for the different table?
    The overlapping methods can be put in a base class or a role used by both.

    Am 2011-05-17 17:14, schrieb Bill Moseley:


    On Tue, May 17, 2011 at 7:43 AM, Peter Edwards wrote:
    Can you say

    my $search_attr = {
    '+columns' => [ { 'email' => \'' } ], # and so on for other missing fields
    };

    then

    my $rs = $schema->search_rs( $query, $search_attr );

    Hi Peter,

    Maybe I'm not understanding your suggestion. The problem I have is an existing application that uses DBIC, and (perhaps incorrectly) there's quite a few direct uses of $user->get_column( 'email' ), I could probably replace those in the app by adding, for example, an "email" method to the result class. There's also other uses of the "email" column such as create() and maybe other resultset methods that verify that every key in the passed in data is an actual column.

    I suspect replacing the get_column() usage is necessary. But, I'm not sure how best to deal with any other methods that might reference a non-existent column.

    Really, what I need to do is prevent the column from ending up in SQL. So, could I defined the non-existent column in the result class and then override the SQL generation somewhere to strip out the columns? I suspect that's less that trivial.

    Other option would be to override insert and create and strip out the non-existent columns.

    Unfortunately, I can't just add the columns to the database.

    Any other ideas?

    Thanks,



    --
    Bill Moseley
    moseley@hank.org

    *"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
    T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
    Handelsgericht Wien, FN 79340b
    *"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
    Notice: This e-mail contains information that is confidential and may be privileged.
    If you are not the intended recipient, please notify the sender and then
    delete this e-mail immediately.
    *"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110518/e04af182/attachment.htm
  • Bill Moseley at May 18, 2011 at 5:01 pm

    On Wed, May 18, 2011 at 2:11 AM, Alexander Hartmaier wrote:

    Why not create another result class for the different table?
    The overlapping methods can be put in a base class or a role used by both.
    That's exactly what I'm doing. I've got two schemas and associated Result
    and ResultSet classes that use Roles to bring in common functionality.

    The problem is this:

    my %new_user = ( first => 'jane', last => 'Doe', email => '
    jane_doe@example.com' );

    my $schema = $user_other_schema ? $alt_schema : $original_schema;

    my $user = $schema->resultset( 'User' )->create( \%new_user );


    Now, if $alt_schema's User class doesn't define an "email", because the
    underling table doesn't have that column, then things blow up.

    So, I'm wondering how to have a "virtual" email column in that class -- or
    some way to make DBIC happy that "email" is not a defined column. Or
    perhaps override create (or lower-level insert or update) and remove that
    column from the passed in data.

    What would be better is to have a layer between the app and the DBIC schema
    (i.e. $model->create_user( \%new_user ) ) where I could handle this easier,
    but this is an existing app and the app currently uses DBIC pretty
    extensively and assumes it has a $schema object to work with.

    Handling $user->email is easy, because I can just add that method. I
    suppose I can override $user->get_column( 'email' ) to filter those out.


    --
    Bill Moseley
    moseley@hank.org
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110518/1a5c26f7/attachment.htm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbix-class @
categoriesperl, catalyst
postedMay 17, '11 at 2:49a
activeMay 18, '11 at 5:01p
posts7
users4
websitedbix-class.org
irc#dbix-class

People

Translate

site design / logo © 2021 Grokbase