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.

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

thanks for the advice, but Eclipse keeps not popping out the automplete on the objects.
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.