phparraysdoctrine-ormdoctrine

Doctrine entity object to array


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

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

Getting entity object ,

Entity\User Object
(
[id:Entity\User:private] => 2
[username:Entity\User:private] => TestUser
[password:Entity\User:private] => 950715f3f83e20ee154995cd5a89ac75
[email:Entity\User:private] => test@test.com
[firm_id:Entity\User:private] => Entity\Firm Object
    (
        [id:Entity\Firm:private] => 16
        [company_name:Entity\Firm:private] => TestFirm
        [company_detail:Entity\Firm:private] => India
        [created_at:Entity\Firm:private] => DateTime Object
            (
                [date] => 2014-08-01 18:16:08
                [timezone_type] => 3
                [timezone] => Europe/Paris
            )

        [user:Entity\Firm:private] => 
    )

[created_at:Entity\User:private] => DateTime Object
    (
        [date] => 2014-08-01 15:12:36
        [timezone_type] => 3
        [timezone] => Europe/Paris
    )

[updated_at:Entity\User:private] => DateTime Object
    (
        [date] => 2014-08-01 15:12:36
        [timezone_type] => 3
        [timezone] => Europe/Paris
    )

[firm:protected] => 
) ,

Tried this ,But as per my requiremnet do not want to user doctrine_query. Thanks.


Solution

  • 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!