【问题标题】:Doctrine object incomplete when no session cookie, why?没有 session cookie 时 Doctrine 对象不完整,为什么?
【发布时间】:2017-05-21 05:08:09
【问题描述】:

今天早上我遇到了一个愚蠢的问题,我很难找到一个体面的解决方案。也许你可以给我小费...

我在实体存储库中有这个简单的函数,带有一个简单的查询:

public function findAvailabilityByDr($delivery_round_id) {
    $qb = $this->_em->createQueryBuilder()
        ->select('partial dro.{id}, 
                  partial drp.{id, maxDeliveries, countDeliveries},
                  partial zdd.{id, day},
                  partial rer.{id, maxOrders, countOrders}')
        ->from($this->_entityName, 'dro')
        ->leftJoin('dro.parts', 'drp')
        ->leftJoin('drp.day', 'zdd')
        ->leftJoin('drp.relayRounds', 'rer')
        ->where('dro.id = :delivery_round_id')
        ->setParameters(array(
            'delivery_round_id' => $delivery_round_id,
        ));

    dump($qb->getQuery()->getOneOrNullResult());
    die();
}

在第一次加载时(没有创建会话 cookie),我得到了一个不完整的结果:

DeliveryRound {#342 ▼
  -id: 117
  -endOfOrdersDate: DateTime {#346 ▶}
  -parts: PersistentCollection {#428 ▼
    -snapshot: array:2 [ …2]
    -owner: DeliveryRound {#342}
    -association: array:15 [ …15]
    -em: EntityManager {#695 …11}
    -backRefFieldName: "deliveryRound"
    -typeClass: ClassMetadata {#373 …}
    -isDirty: false
    #collection: ArrayCollection {#412 ▼
      -elements: array:2 [▼
        0 => DeliveryRoundPart {#425 ▼
          -id: 134
          -deliveryDate: DateTime {#347 ▶}
          -deliveryHourStart: null
          -deliveryHourEnd: null
          -maxDeliveries: null <------ HERE !!!!!!!!!!!
          -countDeliveries: null
          -deliveryRound: DeliveryRound {#342}
          -day: ZoneDeliveryDay {#1302 ▶}
          -ordersCustomers: PersistentCollection {#358 ▶}
          -ordersProviders: PersistentCollection {#410 ▶}
          -relayRounds: PersistentCollection {#637 ▶}
          -roadSheetOptimizationData: null
        }
        1 => DeliveryRoundPart {#1444 ▶}
      ]
    }
    #initialized: true
  }
  -ordersProvidersGenDate: null
  -roadSheetPath: null
  -roadSheetGenDate: null
  -preparationSheetPath: null
  -preparationSheetGenDate: null
  -deliveryMailNotificationsSendingDate: null
  -deliveryNotificationsSendingDate: null
  #translations: PersistentCollection {#420 ▶}
  #newTranslations: null
  #currentLocale: "fr"
  #defaultLocale: "fr"
}

并且在第二次调用(刷新)时,会话 cookie 被创建并且结果是正确的(maxDeliveries: 30 是正确的值):

DeliveryRound {#1657 ▼
  -id: 117
  -endOfOrdersDate: null
  -parts: PersistentCollection {#1794 ▼
    -snapshot: array:2 [ …2]
    -owner: DeliveryRound {#1657}
    -association: array:15 [ …15]
    -em: EntityManager {#695 …11}
    -backRefFieldName: "deliveryRound"
    -typeClass: ClassMetadata {#1673 …}
    -isDirty: false
    #collection: ArrayCollection {#1795 ▼
      -elements: array:2 [▼
        0 => DeliveryRoundPart {#1747 ▼
          -id: 134
          -deliveryDate: null
          -deliveryHourStart: null
          -deliveryHourEnd: null
          -maxDeliveries: 30 <------ HERE !!!!!!!!!!!
          -countDeliveries: 0
          -deliveryRound: DeliveryRound {#1657}
          -day: ZoneDeliveryDay {#2063 ▶}
          -ordersCustomers: PersistentCollection {#1904 ▶}
          -ordersProviders: PersistentCollection {#1896 ▶}
          -relayRounds: PersistentCollection {#2187 ▶}
          -roadSheetOptimizationData: null
        }
        1 => DeliveryRoundPart {#2188 ▶}
      ]
    }
    #initialized: true
  }
  -ordersProvidersGenDate: null
  -roadSheetPath: null
  -roadSheetGenDate: null
  -preparationSheetPath: null
  -preparationSheetGenDate: null
  -deliveryMailNotificationsSendingDate: null
  -deliveryNotificationsSendingDate: null
  #translations: PersistentCollection {#1799 ▶}
  #newTranslations: null
  #currentLocale: "fr"
  #defaultLocale: "fr"
}

当我使用 $qb->getQuery()->getArrayResult() 结果在这两种情况下都是正确的。

WTF?是bug吗?

感谢您的帮助!

编辑

如果我将其简化为最简单的表达式,没有部分和来自控制器的调用,那么错误仍然存​​在......

存储库:

public function findAvailabilityByDr($delivery_round_id) {
    $qb = $this->_em->createQueryBuilder()
        ->select('dro, 
                  drp')
        ->from($this->_entityName, 'dro')
        ->leftJoin('dro.parts', 'drp')
        ->where('dro.id = :delivery_round_id')
        ->setParameters(array(
            'delivery_round_id' => $delivery_round_id,
        ));

    return $qb->getQuery()->getOneOrNullResult();
}

控制器:

class StructureController extends BasePublicCommonController
{
    public function indexAction()
    {
        $delivery_round = $this->getDoctrine()->getManager()->getRepository('ProxymartDeliveryBundle:DeliveryRound')->findAvailabilityByDr(117);
        dump($delivery_round);
        die();
    }
}

这是为了这样的事情,我有时讨厌编程 :)

【问题讨论】:

  • 第一次调用时也返回了deliveryDate,而不是第二次调用...我从来没有在查询中要求过它...似乎学说在第一次调用时改变了我的查询。奇数。
  • 您可以尝试选择drp.*
  • 同样的事情......所有的drp值都是空的。在第二次通话时工作...... :(
  • findAvailabilityByDr() 位于何处?你能粘贴调用它的代码吗?
  • 从服务调用:$delivery_round = $this-&gt;em-&gt;getRepository('ProxymartDeliveryBundle:DeliveryRound')-&gt;findAvailabilityByDr($session_delivery['id']); 两种情况下参数值相同。 DQL 代码也是一样的。为什么当我将结果作为数组 ($qb->getQuery()->getArrayResult()) 时这是正确的,而当我将其作为 Doctrine 对象时是不正确的 ($qb->getQuery()->getResult( ) 或 getOneOrNullResult() ) ?

标签: php session doctrine-orm symfony


【解决方案1】:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/partial-objects.html

我不知道cookie,但我知道arrayResult():

部分对象问题通常不适用于未将查询结果作为对象检索的方法或查询。例如:Query#getArrayResult() [...]

【讨论】:

  • 我将把它用于这个特定的查询,因为这次我不需要持久化数据。但这仍然是一个奇怪的错误。也许我可以尝试更新 php 和 sf。还是谢谢。
  • 或者我应该在未检测到 cookie 时强制重新加载,因为创建会话 cookie 时一切正常。
  • 是的,对不起,我无能为力了
  • 您假设它与 cookie 有关,但我可能不会。尝试清除缓存并查看是否出现相同的错误。 (NOK,OK,OK,OK,缓存清除-> NOK)
  • 好主意,但没有运气,尝试清除开发和生产缓存,手动清空缓存目录...仍然一样。但你是对的:cookie 似乎不是重点。自动重新加载并没有解决问题。我会在我的生产服务器上试一试。这可能是一个本地错误。
猜你喜欢
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2015-07-08
  • 2014-03-04
  • 1970-01-01
  • 2015-11-19
相关资源
最近更新 更多