private function updateEntity($data, $entityName, $id)
{
$vpId = $this->vpidRepository->getVpidForEntity($entityName, $id);
$data['vp_id'] = $vpId;
if ($this->dbSchemaInfo->isChildEntity($entityName)) {
$entityInfo = $this->dbSchemaInfo->getEntityInfo($entityName);
$parentVpReference = "vp_" . $entityInfo->parentReference;
if (!isset($data[$parentVpReference])) {
$table = $this->dbSchemaInfo->getPrefixedTableName($entityName);
$parentTable = $this->dbSchemaInfo->getTableName($entityInfo->references[$entityInfo->parentReference]);
$vpidTable = $this->dbSchemaInfo->getPrefixedTableName('vp_id');
$parentVpidSql = "SELECT HEX(vpid.vp_id) FROM {$table} t JOIN {$vpidTable} vpid ON t.{$entityInfo->parentReference} = vpid.id AND `table` = '{$parentTable}' WHERE {$entityInfo->idColumnName} = {$id}";
$parentVpid = $this->database->get_var($parentVpidSql);
$data[$parentVpReference] = $parentVpid;
}
}
$shouldBeSaved = $this->mirror->shouldBeSaved($entityName, $data);
if (!$shouldBeSaved) {
return;
}
$savePostmeta = !$vpId && $entityName === 'post';
// the post exists in DB for a while but until now it wasn't tracked, so we have to save its postmeta
if (!$vpId) {
$data = $this->vpidRepository->identifyEntity($entityName, $data, $id);
}
$this->mirror->save($entityName, $data);
if (!$savePostmeta) {
return;
}
$postmeta = $this->database->get_results("SELECT meta_id, meta_key, meta_value FROM {$this->database->postmeta} WHERE post_id = {$id}", ARRAY_A);
foreach ($postmeta as $meta) {
$meta['vp_post_id'] = $data['vp_id'];
$meta = $this->vpidRepository->replaceForeignKeysWithReferences('postmeta', $meta);
if (!$this->mirror->shouldBeSaved('postmeta', $meta)) {
continue;
}
$meta = $this->vpidRepository->identifyEntity('postmeta', $meta, $meta['meta_id']);
$this->mirror->save('postmeta', $meta);
}
}