0

I have two tables for my module. First table are: Main table:

value_id | website_id | name

Second table are a relationship of two tables:

value_id | entity_id

My Controller.php:

$postData = $this->getRequest()->getPost();
$ramoModel2 = Mage::getModel('integravet_associar/associacao');
if ($this->getRequest()->getParam('id') <= 0) {
    $ramoModel = Mage::getModel('integravet_associar/associar')->load($this->getRequest()->getParam('id'));
} else {
    $ramoModel = Mage::getModel('integravet_associar/associar');
}

$entitys = $postData['entity_id'];
$ramoModel->setCreatedTime(Mage::getSingleton('core/date')->gmtDate());
$ramoModel->setId_erp($postData['id_erp'])
    ->setName($postData['name'])
    ->setWebsite_id($postData['website_id'])
    ->setId()
    ->save();
$ramoModel2->setId(1)
    ->setEntity_id(1)
    ->save();
$this->_redirect('*/*/');

I can not make it work, what i'm doing wrong?

0
  1. Make sure you have a Resource Model

    Integravet_Associar_Model_Resource_Associacao 
      extends Mage_Core_Model_Resource_Db_Abstract {
            protected function _construct() {
                $this->_init('integravet_associar/tableName', 'primary_id' );
            }
    }
    
  2. And your Model Constructor points to Resource Model

    class Integravet_Associar_Model_Associacao 
      extends Mage_Core_Model_Abstract
    {
           protected function _construct() {
                 parent::_construct();
                 //Resource Name 
                 $this->_init('integravet_associar/associacao ');
           }
    }
    

To do a simple save you don't need _before/_afterSave. With your resource model connected you should be able to put in data.

1
  • Cool if it works go ahead and accept the answer so other folks know and I get these points :-) – Just Name Sep 21 '17 at 19:55
0

Try Below code: You didnt mention anything about id_erp and created_time in your question.

$postData = $this->getRequest()->getPost();
$ramoModel2 = Mage::getModel('integravet_associar/associacao');
if($this->getRequest()->getParam('id') <= 0) {
    $ramoModel = Mage::getModel('integravet_associar/associar')->load($this->getRequest()->getParam('id'));
} else {
    $ramoModel = Mage::getModel('integravet_associar/associar');
}

$entitys = $postData['entity_id'];
$ramoModel->setCreatedTime(Mage::getSingleton('core/date')->gmtDate())
    ->setIdErp($postData['id_erp'])
    ->setName($postData['name'])
    ->setWebsiteId($postData['website_id'])
    ->setId()
    ->save();
$ramoModel2->setValueId(1)
    ->setEntityId(1)
    ->save();
$this->_redirect('*/*/');
8
  • id_erp and created_time not important to relationship. thats why i didn't mentio. – Willian Sugiyama Sep 21 '17 at 15:13
  • Not work... I have read about aftersave and beforesave, and i dont know how to do that in my module... – Willian Sugiyama Sep 21 '17 at 15:15
  • which error you are getting? – Abhishek Panchal Sep 21 '17 at 15:15
  • I don't get a error, but still not saving in table2... – Willian Sugiyama Sep 21 '17 at 15:18
  • Is it saving in Table 1?? – Abhishek Panchal Sep 21 '17 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.