Skip to content

related() returns stale data after update via external model #319

Open
@Clon-cz

Description

@Clon-cz

Version: 3.2.6

Bug Description

When updating a related row using an external repository/model ($this->order_product->find(...)->update(...)), a subsequent call to $parent->related('...')->fetchAll() returns stale (pre-update) data.

However, if I call $relatedRow->update(...) directly on the row from related(), the update is reflected correctly.

Steps To Reproduce

$order = $this->order_db->find($id);
		if ($order) {
			$opPrice = [];
			foreach ($order->related('order_product')->fetchAll() as $op) {
				$opPrice[$op->id]['original'] = $op->price;
//$op->update(['price' => rand(0, 100)]); // works
				$product = $this->order_product->find($op->id);
				$opPrice[$op->id]['new'] = rand(0,100);
				$product->update(["price" => $opPrice[$op->id]['new']]);
			}

			foreach ($order->related('order_product')->fetchAll() as $op) {
				$opPrice[$op->id]['wrong'] = $op->price; // still old
				$opPrice[$op->id]['actual'] = $this->order_product->find($op->id)->offsetGet('price'); // correct
			}
			dump($opPrice);
		}

Important detail

This code works

foreach ($order->related('order_product')->fetchAll() as $op) {
    $op->update(['price' => rand(0, 100)]); // then fetchAll() will reflect the change
}

Database schema

•	order: id
•	order_product: id, order_id, product_id, price

Notes

Looks like internal caching of the related(...) result is not cleared or invalidated when updates happen via external fetches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions