private function _get_many_to_many_post($single_row, $rows, self $foreign_model) { $foreign_singular = $foreign_model->singular(); $col = $foreign_singular . '_ids'; $foreign_plural = $foreign_model->plural(); $foreign_ids = array(); $index = array(); if ($single_row) { $foreign_ids = $rows[$col] ? explode(',', $rows[$col]) : array(); $rows[$foreign_plural] = array(); unset($rows[$col]); } else { foreach ($rows as &$row) { if ($row[$col]) { foreach (explode(',', $row[$col]) as $id) { $foreign_ids[] = $id; $index[$id][] =& $row; } } $row[$foreign_plural] = array(); unset($row[$col], $row); } } $foreign_rows = $foreign_model->many(array('conditions' => array('id' => $foreign_ids))); if ($single_row) { $rows[$foreign_plural] = $foreign_rows; } else { foreach ($foreign_rows as $row) { foreach ($index[$row['id']] as &$r) { $r[$foreign_plural][] = $row; unset($r); } } } return $rows; }