/** * @param array $data * @param bool $exists * @return \Dive\Record */ private function addRecordToCollection(array $data = array(), $exists = false) { $table = $this->userColl->getTable(); $user = $table->createRecord($data, $exists); $this->userColl->add($user); return $user; }
/** * @dataProvider provideOneToMany * * @param bool $authorExists * @param bool $editorExists */ public function testOneToManyReferencedSide($authorExists, $editorExists) { list($user, $userEditor) = $this->createAuthorEditorUsers($authorExists, $editorExists); /** @var Author $author */ $author = $user->Author; /** @var Author $editor */ $editor = $userEditor->Author; // setting reference $authorCollection = new RecordCollection($author->getTable()); $authorCollection->add($author); $editor->Author = $authorCollection; // assertions $this->assertRelationReferences($editor, 'Author', $author); $this->assertEquals($authorCollection, $editor->Author); $user->Author = $author; $userEditor->Author = $editor; $this->assertEquals($user, $userEditor->Author->Author[0]->User); }
/** * @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); }
public function testOneToManyByExchangingRecordCollection() { $user = $this->createUserWithAuthor('JohnD', true); $author = $user->Author; $rm = $user->getRecordManager(); $authorTable = $rm->getTable('author'); $relation = $authorTable->getRelation('Article'); // assert before change /** @var RecordCollection $articles */ $articles = $author->Article; $this->assertCount(3, $articles); // do change, and assert that collection has changed $articleTable = $rm->getTable('article'); $newRecordCollection = new RecordCollection($articleTable); $newRecordCollection->add($articleTable->createRecord()); $author->Article = $newRecordCollection; $this->assertCount(1, $author->Article); // assert original references $originalReferencedIds = $relation->getOriginalReferencedIds($author, 'Article'); $this->assertCount(3, $originalReferencedIds); }
/** * 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); }