Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

php - 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('EntityUser',2);

Getting entity object ,

EntityUser Object
(
[id:EntityUser:private] => 2
[username:EntityUser:private] => TestUser
[password:EntityUser:private] => 950715f3f83e20ee154995cd5a89ac75
[email:EntityUser:private] => [email protected]
[firm_id:EntityUser:private] => EntityFirm Object
    (
        [id:EntityFirm:private] => 16
        [company_name:EntityFirm:private] => TestFirm
        [company_detail:EntityFirm:private] => India
        [created_at:EntityFirm:private] => DateTime Object
            (
                [date] => 2014-08-01 18:16:08
                [timezone_type] => 3
                [timezone] => Europe/Paris
            )

        [user:EntityFirm:private] => 
    )

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

[updated_at:EntityUser: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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(DoctrineORMQuery::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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...