返回一个学说“find()”结果作为一个类

发布于 2024-12-11 23:08:22 字数 812 浏览 0 评论 0原文

因此,我知道 Doctrine“find()”(Doctrine 2)调用的结果会生成基于 Doctrine 的类,但我试图通过 SOAP 连接仅输出相关数据(不要问)并且不希望所有 Doctrine 元数据也通过。

例如,我的 find('Account',1) 结果给了我一个类,其中包含名字、姓氏和电子邮件地址的属性。该类的属性也与硬件模型相关,让我可以利用那里的相关结果。不过,我想要返回的是这样的:

object(stdClass)#15 (5) {
  ["companyName"]=>
  string(12) "test company"
  ["firstName"]=>
  string(5) "chris"
  ["id"]=>
  int(1)
  ["lastName"]=>
  string(7) "smith"
  ["hardware"]=> array(
    [0] => object(stdClass)#15 (5) {
        ["hostname"]=>
        string(12) "host1",
    [1] => object(stdClass)#16 (5) {
        ["hostname"]=>
        string(12) "host2",
    [2] => object(stdClass)#17 (5) {
        ["hostname"]=>
        string(12) "host3"
  )

似乎应该有一种自动方法来执行此类操作,因此我不必编写自定义过滤方法/类来删除我需要的数据。 Doctrine 开发者有什么建议吗?

So, I know that the result of a Doctrine "find()" (Doctrine 2) call results in a Doctrine-based class, but I'm trying to output just the relevant data back over a SOAP connection (don't ask) and don't want all of the Doctrine metadata to come through too.

For example, my result of a find('Account',1) gives me a class back with properties on it for firstName, lastName, and emailAddress. A property on the class also relates to the Hardware model and lets me tap into the associated results there. What I want to be returned, though, is something like:

object(stdClass)#15 (5) {
  ["companyName"]=>
  string(12) "test company"
  ["firstName"]=>
  string(5) "chris"
  ["id"]=>
  int(1)
  ["lastName"]=>
  string(7) "smith"
  ["hardware"]=> array(
    [0] => object(stdClass)#15 (5) {
        ["hostname"]=>
        string(12) "host1",
    [1] => object(stdClass)#16 (5) {
        ["hostname"]=>
        string(12) "host2",
    [2] => object(stdClass)#17 (5) {
        ["hostname"]=>
        string(12) "host3"
  )

It seems like there should be an automatic way to do this sort of thing so I don't have to write a custom filtering method/class just to strip out the data I need. Any tips from Doctrine devs out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-12-18 23:08:22

我认为这可能是您正在寻找的东西:

$em->getRepository('namespace\entity')->createQueryBuilder('a')->where('criteria = 1')->getQuery()->getArrayResult();

但是您需要修改 where.

I think this might be something you are looking for:

$em->getRepository('namespace\entity')->createQueryBuilder('a')->where('criteria = 1')->getQuery()->getArrayResult();

But you need to modify the where.

私藏温柔 2024-12-18 23:08:22

尝试解开相关对象:

$account->setHardware($account->getHardware()->toArray());

但是忘记标准类的东西,获取它的唯一方法是将对象转换为数组,然后将它们转换为对象。不管怎样,如果它是一个 SOAP 连接,我想你应该使用数组而不是对象。

Try to unwrap the related object:

$account->setHardware($account->getHardware()->toArray());

But forget the standard class thing, the only way to get it is to convert your objects to an array and then cast them to objects. Anyway, if it's a SOAP connection, I suppose you should use arrays instead of objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文