simplemap icon indicating copy to clipboard operation
simplemap copied to clipboard

Make it compatible with Vizy Plugin

Open luizboaretto opened this issue 1 year ago • 1 comments

Using this field inside Vizy plugin, I get this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ownerId' cannot be null
The SQL being executed was: INSERT INTO `maps` (`ownerId`, `ownerSiteId`, `fieldId`, `lat`, `lng`, `dateCreated`, `dateUpdated`, `uid`) VALUES (NULL, 1, 15, '-9.6476843', '-35.7339264', '2023-11-21 21:57:21', '2023-11-21 21:57:21', '9118b2a0-2cbe-4433-a20d-032d70233d46')

luizboaretto avatar Nov 21 '23 22:11 luizboaretto

I'm not fully across why Simplemap has its own database table for it's content, but that's the cause of the issue. It assumes that the element that "owns" the field has an ID, which in this case is a fake Block element for Vizy, because Vizy Blocks aren't elements. When it tries to save its content to its own maps table, recording the owner ID, siteId, etc - it won't work, as there is no saved element record for a Vizy block.

One thing that does work is to just not save the record unless there's valid element data.

https://github.com/ethercreative/simplemap/blob/c1a9347914ca949888ab088ed710477ab4beb75c/src/services/MapService.php#L121-L124

Changing to:

$record->lat = $map->lat;
$record->lng = $map->lng;

if (!$record->ownerId) {
    return;
}

$record->save(false);

But I'm not sure the ramifications of doing so. The field value stores fine, as the value is serialized in Vizy's content.

engram-design avatar Nov 24 '23 09:11 engram-design