/** * get foreign controller * * @return Tinebase_Controller_Record_Abstract */ protected function _getController() { if ($this->_controller === NULL) { $this->_controller = Tinebase_Controller_Record_Abstract::getController($this->_options['related_model']); } return $this->_controller; }
/** * transfers relations * * @param string $sourceId * @param string $destinationId * @param string $model * * @return array */ public function transferRelations($sourceId, $destinationId, $model) { $controller = Tinebase_Controller_Record_Abstract::getController($model); // just for validation, the records aren't needed $controller->get($sourceId); $controller->get($destinationId); $tableName = SQL_TABLE_PREFIX . 'relations'; // own side $select = $this->_db->select()->where($this->_db->quoteIdentifier('own_id') . ' = ?', $sourceId); $select->from($tableName); $stmt = $this->_db->query($select); $entries = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); $stmt->closeCursor(); // rel side $select = $this->_db->select()->where($this->_db->quoteIdentifier('related_id') . ' = ?', $sourceId); $select->from($tableName); $stmt = $this->_db->query($select); $relentries = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); $stmt->closeCursor(); $skipped = array(); foreach ($entries as $entry) { $select = $this->_db->select()->where($this->_db->quoteInto($this->_db->quoteIdentifier('own_id') . ' = ?', $destinationId) . ' AND ' . $this->_db->quoteInto($this->_db->quoteIdentifier('related_id') . ' = ?', $entry['related_id'])); $select->from($tableName); $stmt = $this->_db->query($select); $existing = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); $stmt->closeCursor(); if (count($existing) > 0) { $skipped[$entry['rel_id']] = $entry; } else { $this->_dbTable->update(array('own_id' => $destinationId), $this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' = ?', $entry['id'])); } } foreach ($relentries as $entry) { $select = $this->_db->select()->where($this->_db->quoteInto($this->_db->quoteIdentifier('related_id') . ' = ?', $destinationId) . ' AND ' . $this->_db->quoteInto($this->_db->quoteIdentifier('own_id') . ' = ?', $entry['own_id'])); $select->from($tableName); $stmt = $this->_db->query($select); $existing = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); $stmt->closeCursor(); if (count($existing) > 0) { } else { $this->_dbTable->update(array('related_id' => $destinationId), $this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' = ?', $entry['id'])); } } return $skipped; }