I have the following code:
$query = db_query('SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id'); $msg = $query->fetchAllAssoc('id');
Since it is 1:many data, I would expect
id 1 another t1 field another t1 field t2 value 1 more t2 from that row t2 value 2 more t2 from that row id 2
But I only get 1 t2 row for each t1. If I do the same query (without the fetchAllAssoc) in mysql I get the expected results.
Nope fetchAllAssoc() will not do that. A result set from a query is independent of table structure. The db driver doesn't know which columns came from t1 vs. t2, so it can't do what you are asking. They are collapsed into a recordset (table of data) by the query before the results are returned.
You will need to walk the results of the fetch and organize the resulting array yourself.
From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Jeff Greenberg Sent: Thursday, January 16, 2014 11:40 AM To: support@drupal.org Subject: [support] Issues with pdo abstraction with 1:many data
I have the following code:
$query = db_query('SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.idhttp://t1.id = t2.idhttp://t2.id'); $msg = $query->fetchAllAssoc('id');
Since it is 1:many data, I would expect
id 1 another t1 field another t1 field t2 value 1 more t2 from that row t2 value 2 more t2 from that row id 2
But I only get 1 t2 row for each t1. If I do the same query (without the fetchAllAssoc) in mysql I get the expected results. -- --- drupal.org/user/367108http://drupal.org/user/367108 linkedin.com/in/jeffrgreenberghttp://linkedin.com/in/jeffrgreenberg accidentalcoder.comhttp://accidentalcoder.com / ayendesigns.comhttp://ayendesigns.com @accidentalcoder