0

I am new to PHP and PDO. I'm working with Eclipse PDT.

$stmt = $pdo->prepare("SELECT * from articolo");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS, "Articolo");

After that I want to be able to type:

$result[0]->

and from there pressing ctrl+space Eclipse should popup the autocomplete with all the members and functions of that class Articolo.

But nothing happens, as if the IDE doesn't know the Class of $result. Am I doing something wrong? Maybe a cast of $result to (Articolo) is needed?

doing:

$var = new Articolo()
$var->

the autocomplete popup appear correctly.

Simone Conti
  • 544
  • 3
  • 9
  • 20

3 Answers3

1

I'd bet that you just need to give Eclipse a type hint. I'm not an Eclipse user, but you typically use phpdoc and do something along the lines of:

...
$result = $stmt->fetchAll(PDO::FETCH_CLASS, "Articolo");
/** @var $record Articolo */
$record = $result[0];
Nick
  • 6,967
  • 2
  • 34
  • 56
0

Assuming your query was successful & returned more than 1 row, $result contains numerous objects. Each object will be of type Articolo.


To access the object's method(s) you will have to do the following:

$result = $stmt->fetchAll(PDO::FETCH_CLASS, "Articolo");

foreach($result as $object)
{
    // call a method on each object
    $object->someFunction();
} 


Explanation of above code:

  1. Fetch the data, so that $result should contain an array of Articolo objects.
  2. Loop through each object & call a function name that exists in the Articolo Class.

The reason your IDE (Eclipse) did not recognise the functions in the Articolo Class was because you were trying to call a function from the $result variable, which was not of the type Articolo Class.


Some Stack overflow usage tips:

  1. Always use the search in the top right hand corner. Numerous people have come across problems that may could help you.
  2. Always have a look at the How to Ask Questions FAQ
  3. Feedback & always ask further questions if required!
Community
  • 1
  • 1
Haroon
  • 1,125
  • 1
  • 11
  • 13
0

I found a solution:

$articolo = new Articolo();
$stmt = $pdo->prepare("SELECT * from articolo");
$stmt->setFetchMode(PDO::FETCH_INTO, $articolo);
$stmt->execute();           

while ($stmt->fetch()) {
    $articoli[] = clone $articolo;
}           
return $articoli;
Simone Conti
  • 544
  • 3
  • 9
  • 20