berlindb / core

All of the required core code
MIT License
255 stars 29 forks source link

Query::delete_item() does not clean cache #27

Closed felipeelia closed 4 years ago

felipeelia commented 4 years ago

The Problem

When someone calls $query->delete_item( $id ); it deletes the item and then deletes it from the cache. This is done calling $this->clean_item_cache( $item ); (where $item is an object)

The problem is the clean_item_cache() method accepts an array of object items. There is a cast done here but it actually casts the object passed to an array, where each object attribute becomes an index (instead of ending up with an array of objects). That said, the if ( ! is_object( $item ) ) { test is always true (we are iterating over the attributes, not over objects as intended).

Possible Solutions

The possible solutions seem to be simple:

  1. Changing $this->clean_item_cache( $item ); to $this->clean_item_cache( array( $item ) ); fixes the problem.

  2. Changing $items = (array) $items; to if ( ! is_array( $items ) ) { $items = array( $items ); }

Please, let me know if this all makes sense and I'll be more than happy to open a PR. Thanks!