isChildEntity() public method

Returns true if entity has a parent reference.
public isChildEntity ( $entityName ) : boolean
$entityName
return boolean
Ejemplo n.º 1
0
 /**
  * @param $entityName
  * @return mixed
  */
 private function getEntitiesFromDatabase($entityName)
 {
     if ($this->dbSchema->isChildEntity($entityName)) {
         $entityInfo = $this->dbSchema->getEntityInfo($entityName);
         $parentReference = $entityInfo->parentReference;
         return $this->database->get_results("SELECT * FROM {$this->dbSchema->getPrefixedTableName($entityName)} ORDER BY {$parentReference}", ARRAY_A);
     }
     return $this->database->get_results("SELECT * FROM {$this->dbSchema->getPrefixedTableName($entityName)}", ARRAY_A);
 }
Ejemplo n.º 2
0
 /**
  * For standard entities just checks the storage.
  * For child entities (like postmeta) loads all entities and checks them.
  *
  * @param $referencedEntityName
  * @param $referencedEntityId
  * @param $maybeParentId
  * @return bool
  */
 private function entityExists($referencedEntityName, $referencedEntityId, $maybeParentId)
 {
     if (!$this->dbSchemaInfo->isChildEntity($referencedEntityName)) {
         return $this->storageFactory->getStorage($referencedEntityName)->exists($referencedEntityId, null);
     }
     // Optimalization for child entities saved within their parents
     if ($this->storageFactory->getStorage($referencedEntityName)->exists($referencedEntityId, $maybeParentId)) {
         return true;
     }
     $allEntities = $this->storageFactory->getStorage($referencedEntityName)->loadAll();
     return isset($allEntities[$referencedEntityId]);
 }
Ejemplo n.º 3
0
 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);
     }
 }
Ejemplo n.º 4
0
 /**
  * Saves all already existing meta and M:N references for an entity that wasn't tracked yet
  *
  * @param array $data
  * @param string $entityName
  */
 private function storeRelatedEntities($data, $entityName)
 {
     $id = $data[$this->dbSchemaInfo->getEntityInfo($entityName)->idColumnName];
     foreach ($this->dbSchemaInfo->getAllEntityNames() as $referencedEntityName) {
         $entityInfo = $this->dbSchemaInfo->getEntityInfo($referencedEntityName);
         if ($this->dbSchemaInfo->isChildEntity($referencedEntityName) && $entityInfo->references[$entityInfo->parentReference] === $entityName) {
             $childEntities = $this->database->get_results("SELECT * FROM {$this->dbSchemaInfo->getPrefixedTableName($referencedEntityName)} WHERE `{$entityInfo->parentReference}` = '{$id}'", ARRAY_A);
             foreach ($childEntities as $childEntity) {
                 $childEntity = $this->vpidRepository->replaceForeignKeysWithReferences($referencedEntityName, $childEntity);
                 if (!$this->mirror->shouldBeSaved($referencedEntityName, $childEntity)) {
                     continue;
                 }
                 $id = $childEntity[$entityInfo->idColumnName];
                 $vpid = $this->vpidRepository->getVpidForEntity($referencedEntityName, $id);
                 if ($vpid) {
                     $childEntity[$entityInfo->vpidColumnName] = $vpid;
                 } else {
                     $childEntity = $this->vpidRepository->identifyEntity($referencedEntityName, $childEntity, $childEntity[$entityInfo->idColumnName]);
                 }
                 $childEntity = $this->shortcodesReplacer->replaceShortcodesInEntity($referencedEntityName, $childEntity);
                 $this->mirror->save($referencedEntityName, $childEntity);
             }
         }
     }
     foreach ($this->dbSchemaInfo->getAllMnReferences() as $mnReferenceDetails) {
         if ($mnReferenceDetails['source-entity'] === $entityName) {
             $junctionTable = $mnReferenceDetails['junction-table'];
             $prefixedJunctionTable = $this->dbSchemaInfo->getPrefixedTableName($junctionTable);
             $sourceColumn = $mnReferenceDetails['source-column'];
             $references = $this->database->get_results("SELECT * FROM `{$prefixedJunctionTable}` WHERE `{$sourceColumn}` = '{$id}'", ARRAY_A);
             foreach ($references as $reference) {
                 $reference = $this->vpidRepository->replaceForeignKeysWithReferences($junctionTable, $reference);
                 $this->mirror->save($junctionTable, $reference);
             }
         }
     }
 }