public function testGetIdentifiers() { $userOne = $this->addRecordToCollection(); $userTwo = $this->addRecordToCollection(); $expected = array($userOne->getInternalId(), $userTwo->getInternalId()); $actual = $this->userColl->getIdentifiers(); $this->assertEquals($expected, $actual); }
/** * @TODO Remove result collections, use repository records instead * Loads reference for a given record * * @param Record $record * @param string $relationName */ private function loadReferenceFor(Record $record, $relationName) { $recordCollection = $record->getResultCollection(); if (!$recordCollection) { $recordCollection = new RecordCollection($record->getTable()); $recordCollection->add($record); } if ($record->exists()) { $identifiers = $recordCollection->getIdentifiers(); $query = $this->getReferenceQuery($record, $relationName, $identifiers); /** @var \Dive\Record[]|\Dive\Collection\RecordCollection $relatedCollection */ $relatedCollection = $query->execute(RecordManager::FETCH_RECORD_COLLECTION); } else { $table = $record->getTable(); $rm = $table->getRecordManager(); $relatedTable = $this->getJoinTable($rm, $relationName); $relatedCollection = new RecordCollection($relatedTable); } // updates reference map between both collections $isOwningSide = $this->isOwningSide($relationName); $ownerCollection = $isOwningSide ? $relatedCollection : $recordCollection; $referencedCollection = $isOwningSide ? $recordCollection : $relatedCollection; $this->map->updateOwnerCollectionWithReferencedCollection($ownerCollection, $referencedCollection); }
/** * Updates record collections of referenced record (record collection is to be exchanged with another) * * @param Record $record * @param RecordCollection|Record[] $related */ public function updateCollectionReference(Record $record, RecordCollection $related) { $oid = $record->getOid(); // when exchanging collection, we have to unlink all related records $relatedCollection = $this->getRelatedCollection($oid); if ($relatedCollection && $relatedCollection !== $related) { $owningField = $this->relation->getOwningField(); foreach ($relatedCollection as $owningRecord) { $this->removeFieldMapping($owningRecord->getOid()); $owningRecord->set($owningField, null); } } // set references for new related records $this->setReference($record->getInternalId(), $related->getIdentifiers()); if (!$record->exists()) { foreach ($related as $relatedRecord) { $this->setFieldMapping($relatedRecord->getOid(), $oid); } } $this->setRelatedCollection($oid, $related); }