src/Repository.php
public function insertMany($documents, $options = [])
    {
        $insertQuery = [];
        foreach ($documents as $document) {
            $this->classMetadata->getEventManager()->execute(EventManager::EVENT_PRE_INSERT, $document);
            $query = $this->hydrator->unhydrate($document);

            $idGen = $this->classMetadata->getIdGenerator();
            if ($idGen !== null) {
                if (!class_exists($idGen) || !is_subclass_of($idGen, AbstractIdGenerator::class)) {
                    throw new \Exception('Bad ID generator : class \'' . $idGen . '\' not exists or not extends JPC\MongoDB\ODM\Id\AbstractIdGenerator');
                }
                $generator = new $idGen();
                $query['_id'] = $generator->generate($this->documentManager, $document);
            }

            $insertQuery[] = $query;
        }

        $result = $this->collection->insertMany($insertQuery, $options);

        if ($result->isAcknowledged()) {
            foreach ($result->getInsertedIds() as $key => $id) {
                if ($id instanceof \stdClass) {
                    $id = (array) $id;
                }
                $insertQuery[$key]["_id"] = $id;
                $this->hydrator->hydrate($documents[$key], $insertQuery[$key]);

                $this->classMetadata->getEventManager()->execute(EventManager::EVENT_POST_INSERT, $documents[$key]);

                $this->cacheObject($documents[$key]);
            }

            return true;
        } else {
            foreach ($documents as $document) {
                $this->cacheObject($document);
                // $this->documentManager->removeObject();
            }
            return false;
        }
    }
public function insertMany($documents, $options = [])
    {
        $insertQuery = [];
        foreach ($documents as $document) {
            $this->classMetadata->getEventManager()->execute(EventManager::EVENT_PRE_INSERT, $document);
            $query = $this->hydrator->unhydrate($document);

            $idGen = $this->classMetadata->getIdGenerator();
            if ($idGen !== null) {
                if (!class_exists($idGen) || !is_subclass_of($idGen, AbstractIdGenerator::class)) {
                    throw new \Exception('Bad ID generator : class \'' . $idGen . '\' not exists or not extends JPC\MongoDB\ODM\Id\AbstractIdGenerator');
                }
                $generator = new $idGen();
                $query['_id'] = $generator->generate($this->documentManager, $document);
            }

            $insertQuery[] = $query;
        }

        $result = $this->collection->insertMany($insertQuery, $options);

        if ($result->isAcknowledged()) {
            foreach ($result->getInsertedIds() as $key => $id) {
                if ($id instanceof \stdClass) {
                    $id = (array) $id;
                }
                $insertQuery[$key]["_id"] = $id;
                $this->hydrator->hydrate($documents[$key], $insertQuery[$key]);

                $this->classMetadata->getEventManager()->execute(EventManager::EVENT_POST_INSERT, $documents[$key]);

                $this->cacheObject($documents[$key]);
            }

            return true;
        } else {
            foreach ($documents as $document) {
                $this->cacheObject($document);
                // $this->documentManager->removeObject();
            }
            return false;
        }
    }
public function insertMany ( $ documents , $ options = [ ] ) { $ insertQuery = [ ] ; foreach ( $ documents as $ document ) { $ this -> classMetadata -> getEventManager ( ) -> execute ( EventManager :: EVENT_PRE_INSERT , $ document ) ; $ query = $ this -> hydrator -> unhydrate ( $ document ) ; $ idGen = $ this -> classMetadata -> getIdGenerator ( ) ; if ( $ idGen !== null ) { if ( ! class_exists ( $ idGen ) || ! is_subclass_of ( $ idGen , AbstractIdGenerator :: class ) ) { throw new \ Exception ( 'Bad ID generator : class \'' . $ idGen . '\' not exists or not extends JPC\MongoDB\ODM\Id\AbstractIdGenerator' ) ; } $ generator = new $ idGen ( ) ; $ query [ '_id' ] = $ generator -> generate ( $ this -> documentManager , $ document ) ; } $ insertQuery [ ] = $ query ; } $ result = $ this -> collection -> insertMany ( $ insertQuery , $ options ) ; if ( $ result -> isAcknowledged ( ) ) { foreach ( $ result -> getInsertedIds ( ) as $ key => $ id ) { if ( $ id instanceof \ stdClass ) { $ id = ( array ) $ id ; } $ insertQuery [ $ key ] [ "_id" ] = $ id ; $ this -> hydrator -> hydrate ( $ documents [ $ key ] , $ insertQuery [ $ key ] ) ; $ this -> classMetadata -> getEventManager ( ) -> execute ( EventManager :: EVENT_POST_INSERT , $ documents [ $ key ] ) ; $ this -> cacheObject ( $ documents [ $ key ] ) ; } return true ; } else { foreach ( $ documents as $ document ) { $ this -> cacheObject ( $ document ) ; // $this->documentManager->removeObject(); } return false ; } }
Insert multiple documents in collection

@param   mixed   $documents  Documents to insert
@param   array   $options    Options
@return  void
Insert multiple documents in collection