function save_lines(array $post, AOR_Report $bean, $postKey) { $seenIds = array(); if (isset($post[$postKey . 'id'])) { foreach ($post[$postKey . 'id'] as $key => $id) { if ($id) { $aorChart = BeanFactory::getBean('AOR_Charts', $id); } else { $aorChart = BeanFactory::newBean('AOR_Charts'); } $aorChart->name = $post[$postKey . 'title'][$key]; $aorChart->type = $post[$postKey . 'type'][$key]; $aorChart->x_field = $post[$postKey . 'x_field'][$key]; $aorChart->y_field = $post[$postKey . 'y_field'][$key]; $aorChart->aor_report_id = $bean->id; $aorChart->save(); $seenIds[] = $aorChart->id; } } //Any beans that exist but aren't in $seenIds must have been removed. foreach ($bean->get_linked_beans('aor_charts', 'AOR_Charts') as $chart) { if (!in_array($chart->id, $seenIds)) { $chart->mark_deleted($chart->id); } } }