0

Using PHP + Doctrine I get this:

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

Autocomplete within IDE does not suggest methods that follow ->, namely things like getId(), and getServiceType().

And PHP does not offer (easy) casting to desired type....

Dennis
  • 7,907
  • 11
  • 65
  • 115

2 Answers2

1

You will need to typehint for the IDE to know what type of model the repository reutrns.

The getRepository method returns a Doctrine\ORM\EntityRepository, which, when calling findAll() has no idea what type of entity it is you are trying to find (while find all returns a array).

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

Should do the trick.


Edit:
Apparently not all IDE's support this. If that is the case, you can create a typehint comment inside the foreach loop for the $value variant instead:

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

First hint for any of the devs in the team that uses the Jetbrains IDEs and the second for the rest!

Jite
  • 5,761
  • 2
  • 23
  • 37
  • did not work for `$all`, but did for `$value` – Dennis Sep 29 '15 at 18:06
  • Sorry for that, edited the comment to look as it should. I.E., its an `array`, so the `[]` is required for it to be hinted as a list of `ServiceType` objects. – Jite Sep 29 '15 at 18:06
  • well ... tried all of them but does not work for all... still – Dennis Sep 29 '15 at 18:08
  • 1
    Ah, it might be a limitation in Zend studio. Then your typehint on the `$value` solution should work. The above solution should work fine in any of the jetbrains php IDE's though. :) – Jite Sep 29 '15 at 18:10
0

This appears to work (v12.5):

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

This appears to work as well (v13.0):

    /**
     * @var $all ServiceType
     */
    foreach ($all as $key => $value)
       $options[$value->getId()] = $value->getId();
Dennis
  • 7,907
  • 11
  • 65
  • 115