本文介绍了Doctrine 实体对象到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想把学说的entry object转换成普通的数组,这是我目前的代码,

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

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

获取实体对象,

Entity\User Object
(
[id:Entity\User:private] => 2
[username:Entity\User:private] => TestUser
[password:Entity\User:private] => 950715f3f83e20ee154995cd5a89ac75
[email:Entity\User:private] => [email protected]
[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] =>
) ,

尝试了这个,但是根据我的要求,我不想使用 dotric_query.谢谢.

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

推荐答案

你可以试试这样的,

    $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);

另一种方式,

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

Above 将返回一个数组,但包含学说对象.返回数组的最佳方法是使用学说查询.

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

希望这会有所帮助.干杯!

Hope this helps.Cheers!

这篇关于Doctrine 实体对象到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:43