Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Bypass ORM caching
  • Hello,

    I have an Orm model for users. Now I'm trying to write a test case using PHPUnit_Selenium package. I've written several test cases before successfully with Selenium but now I've encountered a problem with ORM's caching mechanisms.

    Code for the test case:
    function test_edit_user_data()
    {
      // Load user data
      $user = Model_User::find(1);
      // Now $user->name is 'Old Name'

      // Using the PHPUnit Selenium functions, make a request to
      // my app that should change $user name
      $this->url('http://example.dev/edit-user-data/');

      // Reload the data
      $user = Model_User::find(1);

      // Verify that the name has is indeed been edited
      $this->assertEquals($user->name, 'New Name'); // here the test fails as $user->name is still 'Old Name'
    }

    If I check the my DB after the test, the user's name is 'New Name'. So I assume the caching in ORM is taking data from cache instead of the DB when I try to reload the data. I tried to reload user data with a different primary key field ( $user = Model_User::find_by_email('user@example.com') ) but this also gives me the old data.

    So is there a way to bypass the cache? Or maybe clear it?
  • HarroHarro
    Accepted Answer
    Correct. A second request for the same data will reproduce the data from cache.

    You can reset the cache from inside the model (as the properties are protected) using:

    $class = get_called_class();
    unset(static::$_cached_objects[$class]);

    You'll have to create a method for that so you can call that from the test.

    Alternatively you can use reflection to make the $_cached_objects accessable for your test, and unset it from there.
  • That solved my problem. Thank you!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion