Beispiel #1
0
 /**
  * @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);
 }
Beispiel #3
0
 /**
  * @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);
 }
Beispiel #5
0
 /**
  * Created new related collection for a given referenced record
  *
  * @param Record $record
  * @return bool|RecordCollection
  * @throws RelationException
  */
 public function createRelatedCollection(Record $record)
 {
     $relationName = $this->relation->getOwningAlias();
     if (!$this->relation->isOneToMany()) {
         throw new RelationException("Reference type for relation '{$relationName}' must be a collection!");
     }
     $oid = $record->getOid();
     $refIds = $this->relation->getRecordReferencedIdentifiers($record, $relationName);
     if (!is_array($refIds)) {
         return false;
     }
     $rm = $record->getTable()->getRecordManager();
     $refTable = $this->relation->getJoinTable($rm, $relationName);
     $collection = new RecordCollection($refTable, $record, $this->relation);
     foreach ($refIds as $refId) {
         if (!$refTable->isInRepository($refId)) {
             return false;
         }
         $relatedRecord = $refTable->getFromRepository($refId);
         $collection->add($relatedRecord);
     }
     $this->setRelatedCollection($oid, $collection);
     return $collection;
 }