1

So I am new to PHP and particularly the world of PDO. I am using PDO to select some data from a table and create an object out of this data. I have been successful with PDO creating a default object and accessing the results. This is the fetch statement for fetching a default object, which works as expected:

$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);

The next thing I want to do is fetch the results and create a custom object out of a predefined class that I have created. I based the below statement on what I was using for the first statement, however, this does not work

$stmt->execute();
$user_object = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');

I have got it working by setting the fetch mode outside of the fetch method and can be seen here:

$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS| PDO::FETCH_PROPS_LATE,'classname');
$object = $stmt->fetch();

I have consulted the PHP manuals but I do not understand why I cannot have a statement like the second one i.e. set the fetchmode within the fetch statement.

Thanks

Update

The following statement:

$user_object = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');
var_dump($stmt->errorInfo());
var_dump($user_object);

Returns -

array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } bool(false)
4
  • 2
    It might be helpful to elaborate on the statement this does not work. E.g. Did you get an error? Did fetch() return false? Or did it return an object but not as expected? Commented Mar 1, 2015 at 19:28
  • Apologies no error was thrown, but when I use var_dump($object) it returns a bool(false) indicating that my object hasn't been created Commented Mar 2, 2015 at 12:03
  • And did the PDO errorInfo() method shed any light? Commented Mar 2, 2015 at 12:38
  • BTW, editing your question to include these additional details will be helpful, plus attract some new attention to the question as it will show up as recently modified in tag feeds. Commented Mar 2, 2015 at 12:39

1 Answer 1

0

You can use this sample of code:

$conn = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$sql = 'SELECT m.id, p.id_product productId, m.margin oldPrice
        FROM prefix_product p
        LEFT JOIN margin m
        ON p.id_product=m.product_id
        WHERE p.id_product=1
        ';
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, Class::class);
return $stmt->fetch();

It returns the Class object initialized with the columns names

Sign up to request clarification or add additional context in comments.

6 Comments

Why don't you mention it's Doctrine and therefore unusable with vanilla PDO?
In my case the entity I want to update is not updatable directly with doctrine. That's why I have to create a custom request. But I can retrieve the connection directly with Doctrine. I just get the connection with doctrine but all the following code is like using PDO directly
But don't you understand that you are writing the answer not for your self, but for someone else? Who don't likely have neither $this->getEntityManager() nor FetchMode::CUSTOM_OBJECT?
I updated the code and get the conn with PDO.
With FetchMode::CUSTOM_OBJECT still in place.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.