Pimcore\Model\Object\Service::pathExists PHP Method

pathExists() public static method

public static pathExists ( $path, $type = null ) : boolean
$path
return boolean
    public static function pathExists($path, $type = null)
    {
        $path = Element\Service::correctPath($path);
        try {
            $object = new AbstractObject();
            if (\Pimcore\Tool::isValidPath($path)) {
                $object->getDao()->getByPath($path);
                return true;
            }
        } catch (\Exception $e) {
        }
        return false;
    }

Usage Example

Example #1
0
 public function correctPath()
 {
     // set path
     if ($this->getId() != 1) {
         // not for the root node
         if ($this->getParentId() == $this->getId()) {
             throw new \Exception("ParentID and ID is identical, an element can't be the parent of itself.");
         }
         $parent = AbstractObject::getById($this->getParentId());
         if ($parent) {
             // use the parent's path from the database here (getCurrentFullPath), to ensure the path really exists and does not rely on the path
             // that is currently in the parent object (in memory), because this might have changed but wasn't not saved
             $this->setPath(str_replace("//", "/", $parent->getCurrentFullPath() . "/"));
         } else {
             // parent document doesn't exist anymore, set the parent to to root
             $this->setParentId(1);
             $this->setPath("/");
         }
         if (strlen($this->getKey()) < 1) {
             $this->setKey("---no-valid-key---" . $this->getId());
             throw new \Exception("Document requires key, generated key automatically");
         }
     } else {
         if ($this->getId() == 1) {
             // some data in root node should always be the same
             $this->setParentId(0);
             $this->setPath("/");
             $this->setKey("");
             $this->setType("folder");
         }
     }
     if (Service::pathExists($this->getFullPath())) {
         $duplicate = AbstractObject::getByPath($this->getFullPath());
         if ($duplicate instanceof self and $duplicate->getId() != $this->getId()) {
             throw new \Exception("Duplicate full path [ " . $this->getFullPath() . " ] - cannot save object");
         }
     }
     if (strlen($this->getFullPath()) > 765) {
         throw new \Exception("Full path is limited to 765 characters, reduce the length of your parent's path");
     }
 }
All Usage Examples Of Pimcore\Model\Object\Service::pathExists