AnsweredAssumed Answered

$bean->save() in a foreach loop

Question asked by Noah Matisoff on Aug 1, 2014
Latest reply on Aug 5, 2014 by Francesca Shiekh
I have written an entry point that looks like the code below.

The data variable receives in POST data from an HTTP request.  I've verified everything is correct on the client-side.

What's wrong with doing something like this?  Can I not have two $bean->save() lines?  I'm trying to loop through each employment history and insert a record for it, and same for address history (and then save the record each time).

What's the problem?

$data = json_decode(file_get_contents('php://input'), true);
$bean = BeanFactory::newBean('EAH_Employment_and_Address_History');
if($data) {
        foreach($data['employment'] as $employment) {
            $bean->name = $data['name'] . ' - Employment';
            $bean->type_c = 'Employment';
            $bean->date_from_c = $employment['df1e'];
            $bean->date_to_c = $employment['dt1e'];
            $bean->company_c = $employment['c1e'];
            $bean->position_c = $employment['p1e'];
            $bean->street_address_city_c = $employment['ci1e'];
            $bean->street_address_state_c = $employment['st1e'];
            $bean->street_address_postalcode_c = $employment['zc1e'];
            $bean->street_address_c = $employment['sa1e'];
        foreach($data['address'] as $address) {
            $bean->name = $data['name'] . ' - Address';
            $bean->type_c = 'Address';
            $bean->date_from_c = $address['df1a'];
            $bean->date_to_c = $address['dt1a'];
            $bean->street_address_city_c = $address['c1a'];
            $bean->street_address_state_c = $address['st1a'];
            $bean->street_address_postalcode_c = $address['zc1a'];
            $bean->street_address_c = $address['sa1a'];
echo json_encode(array('response' => $data));