/** * 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; }