Doctrine findBy* methods and fetch array

↘锁芯ラ 提交于 2019-12-20 02:47:20

问题


What is the cleanest way of using the Doctrine findBy methods but getting an array returned and not objects.

Doctrine::getTable('Table')->findOneById(x);

That works but returns a doctrine object.

I'd still like to be able to use the find methods but I know I can't add

->fetchArray()

on the end.

Anyone else had this problem?


回答1:


Try use toArray

Doctrine::getTable('Table')->findOneById(x)->toArray();



回答2:


You can specify the hydration mode when using magic finders, like so:

Doctrine_Core::getTable('Table')->findOneById($x, Doctrine_Core::HYDRATE_ARRAY);



回答3:


Haim Evgi and DuoSRX's answers are correct, but there's a slightly different version for both that I prefer when using Symfony:

Let's say your model name is Person, you would use:

PersonTable::getInstance()->findOneById(x)->toArray();

or

PersonTable::getInstance()->findOneById($x, Doctrine_Core::HYDRATE_ARRAY);




回答4:


$adCampaign = $em->createQuery('select c from \Model\Campaign c where c.client = ?1')
->setParameter(1, $clientId)
->getArrayResult();

where em is the entityManager - you get the result as array with getArrayResult



来源:https://stackoverflow.com/questions/5552511/doctrine-findby-methods-and-fetch-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!