Pimcore\Model\Version::save PHP Method

save() public method

public save ( ) : void
return void
    public function save()
    {
        \Pimcore::getEventManager()->trigger("version.preSave", $this);
        // check if versioning is disabled for this process
        if (self::$disabled) {
            return;
        }
        if (!$this->date) {
            $this->setDate(time());
        }
        $data = $this->getData();
        // if necessary convert the data to save it to filesystem
        if (is_object($data) or is_array($data)) {
            // this is because of lazy loaded element inside documents and objects (eg: multihref, objects, fieldcollections, ...)
            if ($data instanceof Element\ElementInterface) {
                Element\Service::loadAllFields($data);
            }
            $this->setSerialized(true);
            $data->_fulldump = true;
            $dataString = Serialize::serialize($data);
            // revert all changed made by __sleep()
            if (method_exists($data, "__wakeup")) {
                $data->__wakeup();
            }
            unset($data->_fulldump);
        } else {
            $dataString = $data;
        }
        $this->id = $this->getDao()->save();
        // check if directory exists
        $saveDir = dirname($this->getFilePath());
        if (!is_dir($saveDir)) {
            File::mkdir($saveDir);
        }
        // save data to filesystem
        if (!is_writable(dirname($this->getFilePath())) || is_file($this->getFilePath()) && !is_writable($this->getFilePath())) {
            throw new \Exception("Cannot save version for element " . $this->getCid() . " with type " . $this->getCtype() . " because the file " . $this->getFilePath() . " is not writeable.");
        } else {
            File::put($this->getFilePath(), $dataString);
            // assets are kina special because they can contain massive amount of binary data which isn't serialized, we append it to the data file
            if ($data instanceof Asset && $data->getType() != "folder") {
                // append binary data to version file
                $handle = fopen($this->getBinaryFilePath(), "w", false, File::getContext());
                $src = $data->getStream();
                stream_copy_to_stream($src, $handle);
                fclose($handle);
            }
        }
        \Pimcore::getEventManager()->trigger("version.postSave", $this);
    }

Usage Example

Example #1
0
 /**
  * @param bool $setModificationDate
  * @param bool $callPluginHook
  * @return null|Version
  * @throws \Exception
  */
 public function saveVersion($setModificationDate = true, $callPluginHook = true)
 {
     // hook should be also called if "save only new version" is selected
     if ($callPluginHook) {
         \Pimcore::getEventManager()->trigger("asset.preUpdate", $this, ["saveVersionOnly" => true]);
     }
     // set date
     if ($setModificationDate) {
         $this->setModificationDate(time());
     }
     // scheduled tasks are saved always, they are not versioned!
     $this->saveScheduledTasks();
     // create version
     $version = null;
     // only create a new version if there is at least 1 allowed
     if (Config::getSystemConfig()->assets->versions->steps || Config::getSystemConfig()->assets->versions->days) {
         $version = new Version();
         $version->setCid($this->getId());
         $version->setCtype("asset");
         $version->setDate($this->getModificationDate());
         $version->setUserId($this->getUserModification());
         $version->setData($this);
         $version->save();
     }
     // hook should be also called if "save only new version" is selected
     if ($callPluginHook) {
         \Pimcore::getEventManager()->trigger("asset.postUpdate", $this, ["saveVersionOnly" => true]);
     }
     return $version;
 }