/** * save category * * @todo only INSERTs are supported, UPDATE has to be implemented * @throws ArticleCategoryException */ public function save() { $db = Application::getInstance()->getDb(); if (is_null($this->parentCategory)) { // prepare to insert top level category $rows = $db->doPreparedQuery('SELECT MAX(r) + 1 AS l FROM articlecategories'); $this->l = !isset($rows[0]['l']) ? 0 : $rows[0]['l']; $this->r = $rows[0]['l'] + 1; $this->level = 0; } else { // prepare to insert subcategory // in case parent category has not been saved - save it if (is_null($this->parentCategory->id)) { $this->parentCategory->save(); } try { $nsData = $this->parentCategory->getNsData(); } catch (ArticleCategoryException $e) { throw $e; } $this->l = $nsData['r']; $this->r = $nsData['r'] + 1; $this->level = $nsData['level'] + 1; $db->execute('UPDATE articlecategories SET r = r + 2 WHERE r >= ?', array($this->l)); $db->execute('UPDATE articlecategories SET l = l + 2 WHERE l > ?', array($this->r)); } // insert category data $this->alias = MysqlPDOUtil::getAlias($db, $this->title, 'articlecategories'); $this->id = $db->insertRecord('articlecategories', array('Alias' => $this->alias, 'l' => $this->l, 'r' => $this->r, 'level' => $this->level, 'Title' => $this->title, 'customSort' => $this->customSort)); self::$instancesByAlias[$this->alias] = $this; self::$instancesById[$this->id] = $this; }