Doctrine entity object to array

前端 未结 8 689
星月不相逢
星月不相逢 2020-12-16 13:10

Wants to convert doctrine entiry object to normal array, this is my code so far,

 $demo = $this->doctrine->em->find(\'Entity\\User\',2);
         


        
8条回答
  •  长情又很酷
    2020-12-16 13:55

    You can try something like this,

        $result = $this->em->createQueryBuilder();
        $app_code = $result->select('p')
                ->from('YourUserBundle:User', 'p')
                ->where('p.id= :id')
                ->setParameter('id', 2)
                ->getQuery()
                ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
    

    Another way,

     $this->em->getRepository('YourUserBundle:User')
          ->findBy(array('id'=>1));
    

    Above will return an array but contains doctrine objects. Best way to return an array is using the doctrine query.

    Hope this helps. Cheers!

提交回复
热议问题