Jarves\Objects::getObjectPk PHP Method

getObjectPk() public method

Return only the primary keys of pItem as object.
public getObjectPk ( string $objectKey, array $item ) : string
$objectKey string
$item array
return string
    public function getObjectPk($objectKey, $item)
    {
        $definition = $this->getDefinition($objectKey);
        $result = [];
        foreach ($definition->getPrimaryKeyNames() as $primaryKey) {
            if (isset($item[$primaryKey]) && null !== $item[$primaryKey]) {
                $result[$primaryKey] = $item[$primaryKey];
            }
        }
        if ($result && $definition->getWorkspace()) {
            $result['workspaceId'] = isset($item['workspaceId']) ? $item['workspaceId'] : WorkspaceManager::getCurrent();
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * @param Objects $repo
  * @param $objectKey
  * @param $origItem
  * @return string
  */
 protected function generateDiff(Objects $repo, $objectKey, $origItem)
 {
     $pk = $repo->getObjectPk($objectKey, $origItem);
     $currentItem = $repo->get($objectKey, $pk);
     $definition = $repo->getDefinition($objectKey);
     $changes = [];
     foreach ($definition->getFields() as $field) {
         if ($field->hasFieldType() && !$field->getFieldType()->isDiffAllowed()) {
             //todo, check $field->isDiffAllowed() as well
             continue;
         }
         $k = $field->getId();
         if (!isset($origItem[$k]) || !isset($currentItem[$k])) {
             continue;
         }
         if (!is_string($origItem[$k]) && !is_numeric($origItem[$k])) {
             continue;
         }
         if (!is_string($currentItem[$k]) && !is_numeric($currentItem[$k])) {
             continue;
         }
         $from = strip_tags($origItem[$k]);
         $to = strip_tags($currentItem[$k]);
         if ($from != $to) {
             $htmlDiff = new HtmlDiff($from, $to, true);
             $out = $htmlDiff->outputDiff();
             $changes[$k] = $out->toString();
         }
     }
     $message = [];
     foreach ($changes as $changedKey => $diff) {
         if ($field = $definition->getField($changedKey)) {
             $message[] = ($field->getLabel() ?: $field->getId()) . ': ' . $diff;
         }
     }
     return $message ? '<div class="change">' . implode('</div><div class="change">', $message) . '</div>' : '';
 }
All Usage Examples Of Jarves\Objects::getObjectPk