Array saving data in the database with a field written array

Asked

Viewed 117 times

0

I have two fields saved in the database (in this case it would be as a comment on the request in the Brazilian language, but that doesn’t matter much). These fields are being saved correctly, but with a problem, it saves one of the written fields array. Reference point fields and address type are saved correctly.

HTML code of the fields:

<div class="field small small-left left">
  <label for="comments"><?php echo $this->__('Referência: ') ?></label>
  <div class="input-box">
      <input style="width: 100% !important;" class="input-text" id="comments_1" name="comments[]" title="<?php echo $this->__('Pontos de Referência') ?>" />
   </div>
</div>

   <div class="field info">
    <label for="commentsEnd" class="tipoEnd">Tipo de Endereço:</label>
    <div class="input-box">
       <div class="styled-select">
       <select id="comments_2" name="comments[]" title="<?php echo $this->__('Tipo de Endereço') ?>" class="onestep">
          <option value="">Selecione</option>
          <option value="residencial">Residencial</option>
          <option value="comercial">Comercial</option>
       </select>
       </div>
    </div>

Code in the module Model, which traverses the Array and saves the fields:

if (!empty($oscOrderData['comments'])) {

    $order = $observer->getEvent()->getOrder();
    /* @var $order Mage_Sales_Model_Order */

    // Force array type
    if(!is_array($oscOrderData['comments']))
        $oscOrderData['comments'] = array($oscOrderData['comments']);

    foreach($oscOrderData['comments'] as $comment) {
        $order->addStatusHistoryComment($comment);
    }
    $order->save();
}
  • Which field is saving wrong?

  • @Thiagobarcala What’s happening is that he’s saving an extra field. The two fields are saved correctly, but it saves a more written field array.

  • You may have some other input in the form with that same name (comments[]) and have value "array". Check the browser for the values you submit when you submit the form. If you use Chrome, press F12, click the network tab, and submit the form. You will be able to see the request and all values sent to the server.

  • You can search for comment[] in the Elements tab to see if you have more inputs than you should.

  • What is the database structure and how is getting this wrong record?

1 answer

0


The problem wasn’t having more than one input with the name="comments[]", but was in the duplication of code in the module’s Observer.php file, that is, there were two equal codes and so was saving a field more written Array. I removed one of the codes and the problem was solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.