gpt4 book ai didi

php - Doctrine 实体对象到数组

转载 作者:IT王子 更新时间:2023-10-28 23:52:06 24 4
gpt4 key购买 nike

想要将 doctrine entry 对象转换为普通数组,这是我目前的代码,

 $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] => 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] =>
) ,

已尝试 this ,但根据我的要求,我不想使用 doctrine_query。谢谢。

最佳答案

你可以试试这样的,

    $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 将返回一个数组,但包含 doctrine 对象。返回数组的最佳方式是使用 doctrine 查询。

希望这对您有所帮助。干杯!

关于php - Doctrine 实体对象到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com