Changing data before save so that the data to be saved is an array containing many entities

Hi there,

Using CakePHP 3.7
I am having some problems saving data after changing that data in the beforeSave function of a behavior. I have an upload behavior which automatically uploads files to the server.

All my index, view, delete, add, edit operations run automatically via a component.
Those functions are called like so:
$this->SmartBaseComponent->index($this->Countries, $settings = array());
$this->SmartBaseComponent->addRecord($this->Countries, $settings = array());
$this->SmartBaseComponent->editRecord($this->Countries, $settings = array());
$this->SmartBaseComponent->viewRecord($this->Countries, $settings = array());

And so on. My question is this. In the beforeSave function of a behavior which I wrote to handle file uploads automatically, which was working fine in CakePHP version 2.*, I need to allow the save operation to continue after the entity has been converted to entities. For example: the user uploads a file (UserPics)(id,user-id, filename, … ), I find all the files which the user wishes to upload, validate them, put them into a properly formatted array. Lets say the user upload three pics, they are pass validation, and the data which I need to save would be:

$savePics = array(
0 => array(‘user_id’, ‘filename’=> ‘file-location-etc’),
1 => array(‘user_id’, ‘filename’=> ‘file-location-etc’),
2 => array(‘user_id’, ‘filename’=> ‘file-location-etc’),
);

What triggered the beforeSave in the behavior was a call to $this->UserPics->save($entity);

In the behavior I need to allow save to continue but save the formatted data as above and not just one entity.

I have tried doing this in the beforeSave of the attached behavior:
$useTable = $this->_table;
$entities = $useTable->newEntities($saveRecords, [‘validate’=>false]);
//pr($entities);
//return false;
$result = $useTable->saveMany($entities, [‘validate’=>false]);
return $result;

It does work but instead of it is still saving only the data formatted it saves the unformatted data as well. So I end up with 4 entities saved instead of 3. The fourth entity should not be saved because the actual save operation is based on the initial data submitted for saving etc.

So my question to you guys is this: How can I allow save to continue but actually save the data as formatted by the bahvior etc?

I hope someone on here can help me with this. thanks.