protected function _getRelation($contract, $ipnet)
 {
     $r = new Tinebase_Model_Relation();
     $ra = array('own_model' => 'Sales_Model_Coustomer', 'own_backend' => 'Sql', 'own_id' => $ipnet->getId(), 'own_degree' => 'sibling', 'remark' => 'phpunit test', 'related_model' => 'Sales_Model_Contract', 'related_backend' => 'Sql', 'related_id' => $contract->getId(), 'type' => 'CONTRACT');
     $r->setFromArray($ra);
     return $r;
 }
 /**
  * iterate relations
  * 
  * @param Tinebase_Record_Abstract $currentRecord
  * @return array
  */
 protected function _iterateRelations($currentRecord)
 {
     if (!$currentRecord->relations || get_class($currentRecord->relations) != 'Tinebase_Record_RecordSet') {
         $currentRecord->relations = new Tinebase_Record_RecordSet('Tinebase_Model_Relation');
     }
     $be = new Tinebase_Relation_Backend_Sql();
     // handle relations to remove
     if ($this->_removeRelations) {
         if ($currentRecord->relations->count()) {
             foreach ($this->_removeRelations as $remRelation) {
                 $removeRelations = $currentRecord->relations->filter('type', $remRelation['type'])->filter('related_model', $remRelation['related_model']);
                 $currentRecord->relations->removeRecords($removeRelations);
             }
         }
     }
     // handle new relations
     if ($this->_newRelations) {
         $removeRelations = NULL;
         foreach ($this->_newRelations as $newRelation) {
             $removeRelations = $currentRecord->relations->filter('type', $newRelation['type'])->filter('related_model', $newRelation['related_model']);
             $already = $removeRelations->filter('related_id', $newRelation['related_id']);
             if ($already->count() > 0) {
                 $removeRelations = NULL;
             } else {
                 $newRelation['own_id'] = $currentRecord->getId();
                 $rel = new Tinebase_Model_Relation();
                 $rel->setFromArray($newRelation);
                 if ($removeRelations) {
                     $currentRecord->relations->removeRecords($removeRelations);
                 }
                 $currentRecord->relations->addRecord($rel);
             }
         }
     }
     return $currentRecord->relations->toArray();
 }
 /**
  * iterate relations
  * 
  * @param Tinebase_Record_Interface $currentRecord
  * @return array
  */
 protected function _iterateRelations($currentRecord)
 {
     if (!$currentRecord->relations || get_class($currentRecord->relations) != 'Tinebase_Record_RecordSet') {
         $currentRecord->relations = new Tinebase_Record_RecordSet('Tinebase_Model_Relation');
     }
     // handle relations to remove
     if ($this->_removeRelations) {
         if ($currentRecord->relations->count()) {
             foreach ($this->_removeRelations as $remRelation) {
                 $removeRelations = $currentRecord->relations->filter('type', $remRelation['type'])->filter('related_model', $remRelation['related_model']);
                 $currentRecord->relations->removeRecords($removeRelations);
             }
         }
     }
     // handle new relations
     if ($this->_newRelations) {
         foreach ($this->_newRelations as $newRelation) {
             // convert duplicate to update (remark / degree)
             $duplicate = $currentRecord->relations->filter('related_model', $newRelation['related_model'])->filter('related_id', $newRelation['related_id'])->filter('type', $newRelation['type'])->getFirstRecord();
             if ($duplicate) {
                 $currentRecord->relations->removeRecord($duplicate);
             }
             $newRelation['own_id'] = $currentRecord->getId();
             $rel = new Tinebase_Model_Relation();
             $rel->setFromArray($newRelation);
             $currentRecord->relations->addRecord($rel);
         }
     }
     return $currentRecord->relations->toArray();
 }
 /**
  * update an existing relation
  * 
  * @param  Tinebase_Model_Relation $_relation 
  * @return Tinebase_Model_Relation the updated relation
  */
 public function updateRelation($_relation)
 {
     $id = $_relation->getId();
     $data = $_relation->toArray();
     $data['rel_id'] = $data['id'];
     unset($data['id']);
     unset($data['related_record']);
     if (isset($data['remark']) && is_array($data['remark'])) {
         $data['remark'] = Zend_Json::encode($data['remark']);
     }
     foreach (array($data, $this->_swapRoles($data)) as $toUpdate) {
         $where = array($this->_db->quoteIdentifier('rel_id') . '      = ' . $this->_db->quote($id), $this->_db->quoteIdentifier('own_model') . '   = ' . $this->_db->quote($toUpdate['own_model']), $this->_db->quoteIdentifier('own_backend') . ' = ' . $this->_db->quote($toUpdate['own_backend']), $this->_db->quoteIdentifier('own_id') . '      = ' . $this->_db->quote($toUpdate['own_id']));
         $this->_dbTable->update($toUpdate, $where);
     }
     return $this->getRelation($id, $_relation['own_model'], $_relation['own_backend'], $_relation['own_id']);
 }
 /**
  * adds a new relation
  * 
  * @param   Tinebase_Model_Relation $_relation 
  * @return  Tinebase_Model_Relation|NULL the new relation
  * @throws  Tinebase_Exception_Record_Validation
  */
 protected function _addRelation(Tinebase_Model_Relation $_relation)
 {
     $_relation->created_by = Tinebase_Core::getUser()->getId();
     $_relation->creation_time = Tinebase_DateTime::now();
     if (!$_relation->isValid()) {
         throw new Tinebase_Exception_Record_Validation('Relation is not valid' . print_r($_relation->getValidationErrors(), true));
     }
     try {
         $result = $this->_backend->addRelation($_relation);
     } catch (Zend_Db_Statement_Exception $zse) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not add relation: ' . $zse->getMessage());
         $result = NULL;
     }
     return $result;
 }