Sokil\Mongo\Document::getReferencedDocumentList PHP Method

getReferencedDocumentList() public method

Get document by reference
public getReferencedDocumentList ( string $name ) : null | Document
$name string name of field where reference stored
return null | Document
    public function getReferencedDocumentList($name)
    {
        $referenceList = $this->get($name);
        if (null === $referenceList) {
            return null;
        }
        if (!isset($referenceList[0])) {
            throw new Exception('List of references not found');
        }
        // build list of referenced collections and ids
        $documentIdList = array();
        foreach ($referenceList as $reference) {
            if (empty($reference['$ref']) || empty($reference['$id'])) {
                throw new Exception(sprintf('Iinvalid reference in list for document %s in field %s', $this->getId(), $name));
            }
            $documentIdList[$reference['$ref']][] = $reference['$id'];
        }
        // get list
        $documentList = array();
        $database = $this->collection->getDatabase();
        foreach ($documentIdList as $collectionName => $documentIdList) {
            $documentList += $database->getCollection($collectionName)->find()->byIdList($documentIdList)->findAll();
        }
        return $documentList;
    }