0

i'm started with Symfony 2 and i have problems. The return of my query with ORM returns an object that is not the way I hope. I'm using ORM correctly in my service?

I have this Service

class RequestListener{   
     protected $request;
     protected $em;

     public function __construct(RequestStack $request, EntityManager $em){
        $this->request  = $request->getCurrentRequest();
        $this->em       = $em;
    }


    public function onRequest()
    {

        if ( !$this->request->query->has('apikey') && !$this->request->headers->has('X-API-App-Token') ) {
             throw new BadCredentialsException('No API key found');
        }

        $test = $this->em->getRepository("AutenticacaoBundle\Entity\Sistema")->findOneBy(array('id' => '1'));

        echo "<pre>"; print_r($test);

        die();
    }  
}

and my file service.yml looks like this:

services:
    kernel.listener.request_listner:
        class: AutenticacaoBundle\EventListener\RequestListener
        arguments: ['@request_stack', '@doctrine.orm.entity_manager']
        public: true
        scope: request
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onRequest }

My entity:

class Sistema{
/**
 * @var string
 *
 * @ORM\Column(name="nome", type="string", length=45, nullable=false)
 */
private $nome;

/**
 * @var string
 *
 * @ORM\Column(name="autenticacao", type="string", length=10, nullable=false)
 */
private $autenticacao;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Procob\AutenticacaoBundle\Entity\Usuario", mappedBy="sistema")
 */
private $idUsuario;

/**
 * Constructor
 */
public function __construct()
{
    $this->idUsuario = new \Doctrine\Common\Collections\ArrayCollection();
}


/**
 * Set nome
 *
 * @param string $nome
 * @return Sistema
 */
public function setNome($nome)
{
    $this->nome = $nome;

    return $this;
}

/**
 * Get nome
 *
 * @return string 
 */
public function getNome()
{
    return $this->nome;
}

/**
 * Set autenticacao
 *
 * @param string $autenticacao
 * @return Sistema
 */
public function setAutenticacao($autenticacao)
{
    $this->autenticacao = $autenticacao;

    return $this;
}

/**
 * Get autenticacao
 *
 * @return string 
 */
public function getAutenticacao()
{
    return $this->autenticacao;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Add idUsuario
 *
 * @param \Procob\AutenticacaoBundle\Entity\Usuario $idUsuario
 * @return Sistema
 */
public function addIdUsuario(\Procob\AutenticacaoBundle\Entity\Usuario $idUsuario)
{
    $this->idUsuario[] = $idUsuario;

    return $this;
}

/**
 * Remove idUsuario
 *
 * @param \Procob\AutenticacaoBundle\Entity\Usuario $idUsuario
 */
public function removeIdUsuario(\Procob\AutenticacaoBundle\Entity\Usuario $idUsuario)
{
    $this->idUsuario->removeElement($idUsuario);
}

/**
 * Get idUsuario
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getIdUsuario()
{
    return $this->idUsuario;
}
}

I hope to receive the result of the query formatted in the object but has more fields than I need, and the return is a huge object

returned object:

AutenticacaoBundle\Entity\Sistema Object(
[nome:AutenticacaoBundle\Entity\Sistema:private] => API ANTI FRAUDE
[autenticacao:AutenticacaoBundle\Entity\Sistema:private] => TOKEN
[id:AutenticacaoBundle\Entity\Sistema:private] => 1
[idUsuario:AutenticacaoBundle\Entity\Sistema:private] => Doctrine\ORM\PersistentCollection Object
    (
        [snapshot:Doctrine\ORM\PersistentCollection:private] => Array
            (
            )

        [owner:Doctrine\ORM\PersistentCollection:private] => AutenticacaoBundle\Entity\Sistema Object
        *RECURSION*
        [association:Doctrine\ORM\PersistentCollection:private] => Array
            (
                [fieldName] => idUsuario
                [targetEntity] => AutenticacaoBundle\Entity\Usuario
                [mappedBy] => sistema
                [type] => 8
                [inversedBy] => 
                [isOwningSide] => 
                [sourceEntity] => AutenticacaoBundle\Entity\Sistema
                [fetch] => 2
                [cascade] => Array
                    (
                    )

                [isCascadeRemove] => 
                [isCascadePersist] => 
                [isCascadeRefresh] => 
                [isCascadeMerge] => 
                [isCascadeDetach] => 
                [orphanRemoval] => 
            )

        [em:Doctrine\ORM\PersistentCollection:private] => Doctrine\ORM\EntityManager Object
            (
                [config:Doctrine\ORM\EntityManager:private] => Doctrine\ORM\Configuration Object
                    (
                        [_attributes:protected] => Array
                            (
                                [entityNamespaces] => Array
                                    (
                                        [AutenticacaoBundle] => AutenticacaoBundle\Entity
                                    )

                                [metadataCacheImpl] => Doctrine\Common\Cache\ArrayCache Object
                                    (
                                        [data:Doctrine\Common\Cache\ArrayCache:private] => Array
                                            (
                                                [DoctrineNamespaceCacheKey[sf2orm_default_489a22580630c4a54db185d7439d487df1380fa671bd29dc59b5de5271068a56]] => 1
                                                [sf2orm_default_489a22580630c4a54db185d7439d487df1380fa671bd29dc59b5de5271068a56[AutenticacaoBundle\Entity\Sistema$CLASSMETADATA][1]] => Doctrine\ORM\Mapping\ClassMetadata Object
                                                    (
                                                        [name] => AutenticacaoBundle\Entity\Sistema
                                                        [namespace] => AutenticacaoBundle\Entity
                                                        [rootEntityName] => AutenticacaoBundle\Entity\Sistema
                                                        [customGeneratorDefinition] => 
                                                        [customRepositoryClassName] => 
                                                        [isMappedSuperclass] => 
                                                        [parentClasses] => Array
                                                            (
                                                            )

                                                        [subClasses] => Array
                                                            (
                                                            )

                                                        [namedQueries] => Array
                                                            (
                                                            )

                                                        [namedNativeQueries] => Array
                                                            (
                                                            )

                                                        [sqlResultSetMappings] => Array
                                                            (
                                                            )
  • Please specify why the object you get is not the way you hope, what object would you expect to get. I will make it much easier to help you. – lrnzcig Jul 24 '15 at 18:08
  • I hope to receive the query results formatted in the object, but has more fields than I need, and the return is a huge object. I edited the question and added the entity. – Anderson Ferreira Jul 24 '15 at 18:31
  • print_r often makes objects look bigger than they are. If die(get_class($test)) shows the expected results then go with it. – Cerad Jul 24 '15 at 21:53
  • Did you read this: http://stackoverflow.com/questions/10427282/symfony-2-entitymanager-injection-in-service? maybe helpful – Adib Aroui Jul 25 '15 at 02:52

1 Answers1

1

First check how you create the service because you do not need to inject the request in the constructor. Read this documentation page or this one where tell you how to get the request form the event that you receive in the function that attach the kernel request event.

Other hand, you are using right the entity manager, please explain a bit more what do you get and what do you want get.