processwire-issues icon indicating copy to clipboard operation
processwire-issues copied to clipboard

TableRows: Not able to save a value in an existing TableRow with Page::save().

Open hiboudev opened this issue 1 year ago • 5 comments

Hi, this is not the first time I see this bug, I'm setting a value on the rows and when I save the page the values are lost, I need to use setAndSave() to make it work.

private function saveRatingsToRace(AbstractRacePage $race): void
{
    /** @var TableRows $results */
    $results = $race->raceResults;

    foreach ($results as $result) {
        $result->elorating = 999;
    }

    try {
        $race->setAndSave("raceResults", $results); // Necessary, or the elorating values are not saved.
        // $race->save(); // Not working
    } catch (WireException $e) {
    }
}

PW : 3.0.240 ProField Table : 0.2.5

hiboudev avatar Jul 02 '24 12:07 hiboudev

And if I use: $race->setAndSave(["raceResults" => $results, 'eloNeedsUpdate' => false]); the table raceResults is not saved but eloNeedsUpdate is saved.

So to save this 2 values I have to do:

$race->eloNeedsUpdate = false;
$race->setAndSave(["raceResults" => $results]);
$race->save();

hiboudev avatar Jul 04 '24 14:07 hiboudev

@hiboudev Table is a commercial project (ProFields), not part of the ProcessWire core, so I don't think we're technically allowed to use GitHub for support of it, and instead should use the ProFields support board. But your question is not necessarily specific to Table as the same will be true with other Fieldtypes.

Fieldtypes values can have separate formatted and unformatted values. To me it looks like you might possibly be working with a formatted value ($results)? If so, you'd want to get it via $results = $race->getUnformatted('raceResults'); before manipulating it.

Another possibility is that you have pagination enabled, and thus would always be working with a partial result set that isn't directly saveable, but individual rows are saveable. I don't think this is the case here because it would give you an error message.

Another good possibility is that PW doesn't see the field value as changed, since you set the same exact object instance to the page that is already in the page. So sometimes when working with field values that are objects, you'll have to do a $page->trackChange('raceResults') just to let it know.

ryancramerdesign avatar Jul 05 '24 14:07 ryancramerdesign

@ryancramerdesign I'm already getting the unformatted value, and no pagination is set on this page.

I'll give a try to $page->trackChange('raceResults'), and hope you fill fix this anyway.

hiboudev avatar Jul 06 '24 16:07 hiboudev

$page->trackChange('raceResults') workaround is working.

hiboudev avatar Jul 06 '24 18:07 hiboudev

But the workaround doesn't work everywhere, I have another code where I also need to set the variable to make it update:

$rows = $hotlapPage->hotlapLeaderboard;
// Adding and editing some rows
$hotlapPage->hotlapLeaderboard = $rows; // This is necessary to save the table.
$hotlapPage->trackChange('hotlapLeaderboard'); // This alone doesn't save the table.
$hotlapPage->save();

hiboudev avatar Jul 06 '24 19:07 hiboudev