Pimcore\Model\Document\Service::getUniqueKey PHP Method

getUniqueKey() public static method

public static getUniqueKey ( $item, $nr )
    public static function getUniqueKey($item, $nr = 0)
    {
        $list = new Listing();
        $list->setUnpublished(true);
        $key = Element\Service::getValidKey($item->getKey(), "document");
        if (!$key) {
            throw new \Exception("No item key set.");
        }
        if ($nr) {
            $key = $key . '_' . $nr;
        }
        $parent = $item->getParent();
        if (!$parent) {
            throw new \Exception("You have to set a parent document to determine a unique Key");
        }
        if (!$item->getId()) {
            $list->setCondition('parentId = ? AND `key` = ? ', [$parent->getId(), $key]);
        } else {
            $list->setCondition('parentId = ? AND `key` = ? AND id != ? ', [$parent->getId(), $key, $item->getId()]);
        }
        $check = $list->loadIdList();
        if (!empty($check)) {
            $nr++;
            $key = self::getUniqueKey($item, $nr);
        }
        return $key;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * returns a unique key for an element
  *
  * @param $element
  * @return string
  */
 public static function getUniqueKey($element)
 {
     if ($element instanceof Object\AbstractObject) {
         return Object\Service::getUniqueKey($element);
     } elseif ($element instanceof Document) {
         return Document\Service::getUniqueKey($element);
     } elseif ($element instanceof Asset) {
         return Asset\Service::getUniqueKey($element);
     }
 }