$queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);\r
$mergeMap = array();\r
$flattened = array();\r
-\r
+ \r
foreach($queries as $index => $query)\r
{\r
$this_table = $query['table'];\r
$this_columns = $query['columns'];\r
- \r
- if(!array_key_exists($index, $mergeMap)) {\r
- $prepared = isset($query['prepared']) ? $query['prepared'] : null;\r
- $id = isset($query['id']) ? $query['id'] : null;\r
\r
- $flattened[] = array(\r
- 'columns' => $this_columns,\r
- 'table' => $this_table,\r
- 'prepared' => $prepared,\r
- 'id' => $id\r
- );\r
- }\r
\r
for($i = $index+1; $i<count($queries); $i++)\r
{\r
//need to keep track of what we merged as future queries might reference the old ids.\r
$mergeMap[$i] = $index;\r
}\r
+ \r
+ //XXX: Another thing that might happen is we have to create queries running on the same table, but with unique columns.\r
+ //In this case, we can take the columns of one and put it into the other. Otherwise we create two records when we really\r
+ //should have only one. An example of this is when a user is created, a query to add the country to users_meta is run,\r
+ //and then _another_ to add firstname, lastname and user_id. It should really all be done in one query.\r
+ \r
+ //Make sure both queries are for the same table, and the both relate back to the main query\r
+ if($this_table == $queries[$i]['table'] && in_array('%MAIN_QUERY_ID%', $this_columns) && in_array('%MAIN_QUERY_ID%', $queries[$i]['columns']))\r
+ {\r
+ $this_column_names = array_keys($this_columns);\r
+ $other_column_names = array_keys($queries[$i]['columns']);\r
+ $combine = true;\r
+ foreach($this_column_names as $column_name)\r
+ {\r
+ if($this_columns[$column_name] != '%MAIN_QUERY_ID%' && in_array($column_name, $other_column_names))\r
+ {\r
+ $combine = false;\r
+ }\r
+ }\r
+ \r
+ if($combine)\r
+ {\r
+ $this_columns = array_merge($this_columns, $queries[$i]['columns']);\r
+ $mergeMap[$i] = $index;\r
+ }\r
+ }\r
}\r
}\r
+\r
+ if(!array_key_exists($index, $mergeMap)) {\r
+ $prepared = isset($query['prepared']) ? $query['prepared'] : null;\r
+ $id = isset($query['id']) ? $query['id'] : null;\r
+\r
+ $flattened[] = array(\r
+ 'columns' => $this_columns,\r
+ 'table' => $this_table,\r
+ 'prepared' => $prepared,\r
+ 'id' => $id\r
+ );\r
+ }\r
}\r
\r
$queries = array();\r
\r
$queries[] = $query;\r
}\r
- \r
+\r
// if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)\r
// {\r
$idMap = [];\r