Jump to content

How to add data into database through my module


Recommended Posts

Hello everyone,

 

 

I want to import products from my company's framework into prestashop.

I am using web service of company's framework and it is giving me JSON and i successfully converted to array. Now I want to add that array into database.

 

SO please help me in adding it.
I am not getting where to put my code. and which method to use for that.

1. Do i need to use Db::getInstance() method with insert()?

 

2. How to call a method having the insertion code from my module.?and also tell me how to define that method? Can i just add my code into  hook?

 

 

In my module.tpl file i have a save button. so what i need to place in _saveContent() method?can i place my insertion code into this function?
Thanks in advance... :)

Link to comment
Share on other sites

Can you briefly described what kind of module you created? You used a tpl for the back office, and not the getContent method?

 

Yes here is my getContent method and saveContent method

 

public function getContent()

  {

    $message = '';

 

    if (Tools::isSubmit('submit_'.$this->name))

    {

        

      $message = $this->_saveContent();

      $message = $this->displayConfirmation($this->l('msg in response'));

    

    }

 

    $this->_displayContent($message);

 

    return $this->display(__FILE__, 'settings.tpl');

  }  

 

  private function _saveContent()

  {

    $message = '';

 

    if (!$message)

    {

      $message = $this->displayConfirmation($this->l('Your settings have been saved from'));      

    }

    else

      $message = $this->displayError($this->l('There was an error while saving your settings'));

 

    return $message;

  }

 

 

 

 

And i have a buuton in my settings.tpl file

 

it is:

 

<fieldset>

  <legend>Importing from framework</legend>

  <form method="post">    

    <p>

      <label>Click this to import</label>

      <input id="submit_{$module_name}" name="submit_{$module_name}" type="submit" value="Save" class="button" />

    </p>

  </form>

</fieldset>

Edited by Sanket (see edit history)
Link to comment
Share on other sites

Ah well in this case in your saveContent method you only need to use  Db::getInstance()->insert... as you correctly pointed out :) You can simply use that

 

like

 private function _saveContent()
  {
    $message = '';

    $data = array('''your data here!! one key for each column);

    if(Db::getInstance()->insert($data))
      $message = $this->displayConfirmation($this->l('Your settings have been saved from'));     
    else
      $message = $this->displayError($this->l('There was an error while saving your settings'));

    return $message;
  }
 
Link to comment
Share on other sites

  • 1 year later...
×
×
  • Create New...