/** * testOffsetUnset(). */ public function testOffsetUnset() { unset($this->object[1]); unset($this->object[3]); $this->assertEquals(2, count($this->object)); $this->assertEquals(array(0), $this->object->getIdLessIndexes(), 'wrong idLess indexes'); $this->assertEquals(array(1), $this->object->getArrayOfIds(), 'wrong idFull indexes'); }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $recordSet * @return Tinebase_Record_RecordSetDiff */ public function diff($recordSet) { if (!$recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Did not get Tinebase_Record_RecordSet, skipping diff(' . $this->_recordClass . ')'); } return new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName())); } if ($this->getRecordClassName() !== $recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $existingRecordsIds = $this->getArrayOfIds(); $toCompareWithRecordsIds = $recordSet->getArrayOfIds(); $removedIds = array_diff($existingRecordsIds, $toCompareWithRecordsIds); $addedIds = array_diff($toCompareWithRecordsIds, $existingRecordsIds); $modifiedIds = array_intersect($existingRecordsIds, $toCompareWithRecordsIds); $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = new Tinebase_Record_RecordSet('Tinebase_Record_Diff'); foreach ($addedIds as $id) { $added->addRecord($recordSet->getById($id)); } // consider records without id, too foreach ($recordSet->getIdLessIndexes() as $index) { $added->addRecord($recordSet->getByIndex($index)); } foreach ($removedIds as $id) { $removed->addRecord($this->getById($id)); } // consider records without id, too foreach ($this->getIdLessIndexes() as $index) { $removed->addRecord($this->getByIndex($index)); } foreach ($modifiedIds as $id) { $diff = $this->getById($id)->diff($recordSet->getById($id)); if (!$diff->isEmpty()) { $modified->addRecord($diff); } } $result = new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName(), 'added' => $added, 'removed' => $removed, 'modified' => $modified)); return $result; }