教义实体对象到数组

2022-01-16 00:00:00 arrays php doctrine doctrine-orm

想要将教义实体转换为普通数组,这是我目前的代码,

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

 $demo = $this->doctrine->em->find('EntityUser',2);

获取实体对象,

EntityUser Object
(
[id:EntityUser:private] => 2
[username:EntityUser:private] => TestUser
[password:EntityUser:private] => 950715f3f83e20ee154995cd5a89ac75
[email:EntityUser:private] => test@test.com
[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] => 
) ,

试过 这个 ,但根据我的requiremnet,不想使用doctrine_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(DoctrineORMQuery::HYDRATE_ARRAY);

另一种方式,

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

相关文章