Sokil\Mongo\Collection::getDocuments PHP Méthode

getDocuments() public méthode

Get documents by list of id
public getDocuments ( array $idList, callable $callable = null ) : array | null
$idList array list of ids
$callable callable cursor callable used to configure cursor
Résultat array | null
    public function getDocuments(array $idList, $callable = null)
    {
        $idListToFindDirectly = $idList;
        // try to egt document from pool if enabled
        $documentsInDocumentPool = array();
        if ($this->isDocumentPoolEnabled() && !$callable) {
            $documentsInDocumentPool = $this->getDocumentsFromDocumentPool($idList);
            if (count($documentsInDocumentPool) === count($idList)) {
                return $documentsInDocumentPool;
            }
            // skip ids already found in pool
            $idListToFindDirectly = array_diff_key(array_map('strval', $idList), array_keys($documentsInDocumentPool));
        }
        // get documents directly
        $cursor = $this->find();
        if (is_callable($callable)) {
            call_user_func($callable, $cursor);
        }
        $documentsGettingDirectly = $cursor->byIdList($idListToFindDirectly)->findAll();
        if (!$documentsGettingDirectly) {
            return $documentsInDocumentPool ? $documentsInDocumentPool : array();
        }
        if ($this->isDocumentPoolEnabled()) {
            $this->addDocumentsToDocumentPool($documentsGettingDirectly);
        }
        return $documentsGettingDirectly + $documentsInDocumentPool;
    }

Usage Example

Exemple #1
0
 public function testGetDocuments_UnexistedIdsSpecified()
 {
     // get documents when wrong id's
     $this->assertEquals(array(), $this->collection->getDocuments(array(new \MongoId(), new \MongoId(), new \MongoId())));
 }