Persistence_Array adding records instead of updating

#1

I’m using a Persistence_Array to calculate a temporary table for display (not that it matters, but I’m processing timecard data and display one column per day, week, or month, depending on what the user’s set, so the number of columns is variable, which is why I’m not using a database table for it), and I’ve come upon an odd behavior.

In my code I cycle through each employee and then through each data range, using addCondition to restrict the data range. All of that works just fine, and results in a code that looks like:

$array = [];
$persistence = new \atk4\data\Persistence_Array($array);
$arraymodel = new \atk4\data\Model($persistence);

while ($date < $enddate)
{
// set up model to query the DB for the appropriate timecards for the current period
foreach ($empl as $id => $item)
{
$arraymodel->tryload($id);
$arraymodel[$arrymodel->id_field] = $id;
$arraymodel[‘Name’] = $item[‘Name’];
$arraymodel[‘Period_’ . $period] = $item[‘Total’];
$arraymodel->save();
}

$date = next period date; // however this is calculated
$period = $period + 1;
}

Pretty straightforward, right? But here’s the weird part. If I run that code exactly as is above, then it executes just fine – but every time i do the $arraymodel->save() it creates a new record. It’s as if it’s ignoring the $id entirely. However, if I insert something like:

foreach ($empl as $id => $item)
{
$array[$id] = [‘id’ => $id];
}

right after I create the array, but before I associate it with the persistence, then the code works perfectly – I end up with one record per employee, with all the right period data in the right place.

Am I doing something wrong? I can’t see why my code would work if and only if I initialize that array.

#2

please simplify example and report as a bug in github.