upsertOne() public method

Upserts an object (updates if exists, insert if not exists)
public upsertOne ( array $cond, array $data, callable $cb = null, array $params = null ) : void
$cond array Conditions
$data array Data
$cb callable Optional. Callback called when response received
$params array Optional. Params
return void
Example #1
0
    /**
     * @param $block
     * @param bool $update
     */
    public function saveBlock($block, $update = false)
    {
        $block['mtime'] = microtime(true);
        if (!isset($block['locale'])) {
            $block['locale'] = null;
        }
        if (isset($block['_id'])) {
            $find = array('_id' => $block['_id']);
        } elseif (isset($block['path'])) {
            $find = array('locale' => $block['locale'], 'path' => $block['path']);
        } else {
            $find = array('name' => (string) $block['name']);
        }
        unset($block['_id']);
        if (isset($block['template'])) {
            $tpl = $this->appInstance->getQuickyInstance();
            $tpl->register_function('getblock', function ($args) {
            });
            $tpl->load_filter('pre', 'optimize');
            $block['templatePHP'] = 'return function($tpl) {
			$var = &$tpl->_tpl_vars;
			$config = &$tpl->_tpl_config;
			$capture = &$tpl->_block_props[\'capture\'];
			$foreach = &$tpl->_block_props[\'foreach\'];
			$section = &$tpl->_block_props[\'section\'];
			?>' . $tpl->_compile_string($block['template'], implode(':', $find)) . '<?php };';
        }
        $block['cachekey'] = md5($block['templatePHP']);
        $this->blocks->upsertOne($find, $update ? array('$set' => $block) : $block);
    }