doctrine/mongodb

Find not working

gerMdz opened this issue · 2 comments

Good day:
When making a query of the type

$ document = $ this-> manager-> createQueryBuilder (Physical Document :: class)
-> field ('id') -> equals ($ id)
-> getQuery ()
-> getSingleResult ();

and then

$ clse = get_class ($ document);

I get an error

I can't get an empty element class.
However, if I consult directly at the BD if I obtain data.

Use
Symfony 4.4
"doctrine / mongodb-odm-bundle": "^ 4.1",

MongoDB extension version => 1.6.1
libbson bundled version => 1.15.2

Mongodb V 3.4.13

namespace Argws\MongoDocsBundle\Controller;

use Argws\DocumentosBundle\Entity\Documento;
use Argws\MongoDocsBundle\Document\DocumentoFisico;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class DocumentoFisicoAction extends AbstractController
{
public $requestStack;
public $entityManager;
public $container;
private $manager;

public function __construct(
    RequestStack $requestStack,
    EntityManagerInterface $entityManager,
    ContainerInterface $container,
    DocumentManager $documentManager

) {

    $this->requestStack = $requestStack;
    $this->entityManager = $entityManager;
    $this->container = $container;
    $this->manager = $documentManager;

}

public function __invoke()
{

    $id_documento = $this->requestStack->getCurrentRequest()->get('id');
    $documento = $this->entityManager->getRepository(Documento::class)->find($id_documento);
    if (!$documento) {
        return new JsonResponse('No encontrado el doc principal: -> '. $documento->getId(), 404);
    }

    $df = $this->buscaDocumentoFisico($documento->getId());


    if (!$df) {
        return new JsonResponse('No encontrado físico: ->'. $documento->getId() , 404);
    }



        $contenido = $df->getDocumento()->getBytes();
        return new Response(
            $contenido, 200, [
                'Content-Type' => $df->getTipoMime(),
                'Content-Disposition' => 'filename=' . $df->getTitulo()
            ]
        );


    return new JsonResponse(null, 404);
}

/**
 * Retorna el DocumentManager
 *
 * @return DocumentManager
 */
private function getDocumentManager()
{

    return $this->get('doctrine_mongodb.odm.document_manager');

}

private function buscaDocumentoFisico($id){

    $documento = $this->manager->getRepository(DocumentoFisico::class)->findOneBy(array('id_documento'=>$id));

/* $documento = $this->manager->createQueryBuilder(DocumentoFisico::class) /
/
->field('id_documento')->equals($id) /
/
->limit(1) /
/
->getQuery() /
/
->getSingleResult(); */

    $clse = get_class($documento);

    return $documento;

}

}

Hello! First, your issue is not about doctrine/mongodb but about doctrine/mongodb-odm. Second, your post has several formatting issues (code fencing being one) and does not clearly say what the problem is. Last as this is a support question rather than a bug in the library please consider posting a question on https://stackoverflow.com/ and tagging it as doctrine-odm.

Gracias