/** * @param null | int $ritem * @return bool */ protected function _addRelation($ritem = null) { $query = $this->db->getQuery(true); // get items $query->select('id, params')->from('#__zoo_relateditemsproxref')->where('item_id = ' . (int) $ritem)->where('ritem_id =' . (int) $this->_item->id)->where('element_id=' . $this->db->Quote($this->_identifier)); $existing = $this->db->setQuery($query)->loadAssoc(); $params = $this->_item->params->get('relateditemspro.', array()); $params = json_encode($params); // If params are not the same if (isset($existing) && array_key_exists('params', $existing) && @$existing['params'] != $params) { // remove relation so we can reinsert it with the right params $query->clear()->delete('#__zoo_relateditemsproxref')->where('item_id = ' . (int) $ritem)->where('ritem_id =' . (int) $this->_item->id)->where('element_id=' . $this->db->Quote($this->_identifier)); $this->db->setQuery($query)->execute(); } // If the relation wasn't existing or the params weren't the same if (!$existing || @$existing['params'] != $params) { // add relation if entry does not exist $query->insert('#__zoo_relateditemsproxref')->set('item_id = ' . (int) $ritem)->set('ritem_id = ' . (int) $this->_item->id)->set('element_id= ' . $this->db->Quote($this->_identifier))->set('params = ' . $this->db->Quote($params)); $this->db->setQuery($query)->execute(); } return true; }