php Doctrine findBy* 方法和获取数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5552511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine findBy* methods and fetch array
提问by sj433
What is the cleanest way of using the Doctrine findBy methods but getting an array returned and not objects.
使用 Doctrine findBy 方法但返回数组而不是对象的最干净的方法是什么。
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
我仍然希望能够使用 find 方法,但我知道我无法添加
->fetchArray()
on the end.
最后。
Anyone else had this problem?
还有其他人有这个问题吗?
回答by DuoSRX
You can specify the hydration mode when using magic finders, like so:
您可以在使用魔术探测器时指定水化模式,如下所示:
Doctrine_Core::getTable('Table')->findOneById($x, Doctrine_Core::HYDRATE_ARRAY);
回答by Steven Mercatante
Haim Evgi and DuoSRX's answers are correct, but there's a slightly different version for both that I prefer when using Symfony:
Haim Evgi 和 DuoSRX 的答案是正确的,但是在使用 Symfony 时,我更喜欢两者的版本略有不同:
Let's say your model name is Person, you would use:
假设您的模型名称是 Person,您将使用:
PersonTable::getInstance()->findOneById(x)->toArray();
PersonTable::getInstance()->findOneById(x)->toArray();
or
或者
PersonTable::getInstance()->findOneById($x, Doctrine_Core::HYDRATE_ARRAY);
PersonTable::getInstance()->findOneById($x, Doctrine_Core::HYDRATE_ARRAY);
回答by alex toader
$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
其中 em 是 entityManager - 您使用 getArrayResult 将结果作为数组