As far as I know, in PDO there is no support directly in PDO. Usually, if you need to create a graph of a complex object from the result of a query that is responsible for ORM.
If you need this functionality, I recommend using Doctrine or Propel , rather than writing something yourself. There are others that may be easier, but I have no experience with them.
EDIT:
I think that perhaps I misunderstood this question, as I am sure that others can. I think the real question was how to access the joined columns, not how to create an object from them.
In this case, simply using the standard arry fethc method, such as PDO::FETCH_ASSOC , PDO::FETCH_NUMERIC or PDO::FETCH_BOTH , you will get all the columns that you requested.
Therefore, if you want to turn this into an "object graph", you need to do it manually without using PDO::FETCH_CLASS .
For example:
//$db is pdo: // also notice im aliase the columns prefixing the name so that we can tell what belongs to // post and what belongs to user, an alternative approach would be to use FETCH_NUMERIC, // which just uses the column positions from the seelct statement as the keys // so in this case post.id would be in the array as key 0, and user.name would be in the // array as key 4 $stmt = $db->prepare('SELECT post.id as p_id, post.text as p_text, post.user_id as p_user_id, user.id as u_id, user.name as u_name FROM POST INNER JOIN User on post.user_id = user.id'); $stmt->execute(); while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { print_r($row); /* will output: Array ( 'p_id' => 'value' 'p_text' => 'value' 'p_user_id' => 'value' 'u_id' => 'value', 'u_name' => 'value' ) So now you need to decide how to create your objects with the information returned */ }
prodigitalson
source share