Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Reloading CDBI objects from database elegantly

by perrin (Chancellor)
on May 19, 2005 at 20:05 UTC ( #458762=note: print w/replies, xml ) Need Help??


in reply to Reloading CDBI objects from database elegantly

This is not a cache, nor is it related to the "live objects index" that people sometimes confuse with a cache. The issue is that you have an object, populated with data, and it stays that way unless you do something to it. All database/object mapping tools work this way.

All you need to do is get rid of the object and load it again. If you let it go out of scope or undef it and then call the constructor (retrieve) again, you will get fresh data.

  • Comment on Re: Reloading CDBI objects from database elegantly

Replies are listed 'Best First'.
Re^2: Reloading CDBI objects from database elegantly
by EvdB (Deacon) on May 19, 2005 at 20:29 UTC

    Quite right, as the following code shows:

    use strict; use warnings; package Foo; use base 'Class::DBI'; __PACKAGE__->connection( 'dbi:SQLite:dbname=db_file', '', '' ); __PACKAGE__->table('foo_table'); __PACKAGE__->columns( All => qw/id name/ ); # create table foo_table ( id integer primary key, name char(10) ); 1; package main; use Test::More 'no_plan'; # In test suite. my $cdbi_object = Foo->create( { name => 'Foo' } ); # Check CDBI object is as expected. is $cdbi_object->name, 'Foo', "name is correct (Foo)"; # Change the name through web site. ok( Foo->db_Main->do("update foo_table set name = 'Bar'"), "update name to 'Bar'" ); # undef and reload object. ok my $cdbi_id = $cdbi_object->id, "get id"; $cdbi_object = undef; ok $cdbi_object = Foo->retrieve($cdbi_id), "retrieve object"; # Check that the name has changed. is $cdbi_object->name, 'Bar', "name has changed to 'Bar'"; ok( $_->delete, "delete object" ) for Foo->retrieve_all;

    Still it would b e nice if this could be done more easily with something like $object->freshen.

Re^2: Reloading CDBI objects from database elegantly
by exussum0 (Vicar) on May 19, 2005 at 21:56 UTC
    Are you saying that some ORMs let you,
    1. Create an object.
    2. Store it.
    3. Retrieve it into some variable a.
    4. Retrieve it again into some variable b.
    5. A change to a won't change b?

    ----
    Give me strength for today.. I will not talk it away..
    Just for a moment.. It will burn through the clouds.. and shine down on me.

      All of the perl ones work that way, except for the latest version of Class::DBI which does some tricks with an index of loaded objects and weakrefs to avoid this. And that has caused so many user complaints that I'm hoping to turn it off in the next release.

      ORM or not, once you retrieve data from the database and hold it in a variable, it has no way of knowing that the underlying database has been changed. This usually doesn't matter since the data is kept in variables for only a short time. Where it does matter, people have to use locking schemes to deal with it.

        Thanks for the info on Class::DBI.

        But it really depends on the ORM. Having delt with ones inside and outside of perl, EJB and I think Hibernate both support multiple instances of an object, but modifying one means you modify the other for persistable things. Hibernate doesn't do it as thoroughly as EJB.

        I'm really surprised it has caused complaints. What have you been told?

        -s

        ----
        Give me strength for today.. I will not talk it away..
        Just for a moment.. It will burn through the clouds.. and shine down on me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://458762]
help
Chatterbox?
LanX will call his fifth son "Edsger"
[karlgoethebier]: erix: ...there is still the other Donald
LanX unfair Knuth has a "pipe organ"
LanX has only an ordinary nose.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-12-12 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (335 votes). Check out past polls.

    Notices?