Doctrine/PHP 集成是否在 IDE 中提供自动完成功能?

Does Doctrine/PHP integration offer Autocomplete in IDE?

使用 PHP + Doctrine 我得到这个:

    //retrieve data
    $entityManager = $this->getEntityManager();
    $all = $entityManager->getRepository('\Entity\ServiceType')->findAll();
    foreach($all as $value)
        $options[$value->getId()] = $value->getServiceType();

IDE 中的自动完成不建议遵循 -> 的方法,即 getId()getServiceType().

之类的方法

并且 PHP 不提供(简单)转换为所需类型....

您需要为 IDE typehint 以了解存储库返回的模型类型。

getRepository 方法 returns a Doctrine\ORM\EntityRepository,调用 findAll() 时不知道您要查找的是什么类型的实体(同时查找所有 returns 个 array)。

/** @var \Entity\ServiceType[] $all */
$all = $entityManager->getRepository('\Entity\ServiceType')->findAll();

应该可以解决问题。


编辑:
显然并非所有 IDE 都支持这一点。 如果是这种情况,您可以在 foreach 循环内为 $value 变体创建类型提示注释:

/** @var \Entity\ServiceType[] $all */
$all = $entityManager->getRepository('\Entity\ServiceType')->findAll();
foreach($all as $value) {
    /** @var \Entity\ServiceType $value */
    $options[$value->getId()] = $value->getServiceType();
}

第一个提示给团队中任何使用 Jetbrains IDE 的开发人员,第二个提示给其他人!

这似乎有效 (v12.5):

    foreach ($all as $key => $value)
        /**
         * @var $value ServiceType
         */
        $options[$value->getId()] = $value->getId();

这似乎也有效 (v13.0):

    /**
     * @var $all ServiceType
     */
    foreach ($all as $key => $value)
       $options[$value->getId()] = $value->getId();