Magento 1.9 – How to Insert in Custom Table

custommagento-1.9module

(Sorry if my english is bad, but is not my language)

My Magento version is 1.9.3.8

I have a custom module that get data when a costumer make an order. I have no problems to get all the information and put all in a json (all works here).

Now i am try to insert that json and order id in a custom table, but when I try to insert i have this error a below:

eSQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect
table name '', query was: INSERT INTO “ () VALUES ()

Here my code.

config.xml (Mage/Shop/etc/config.xml)

<config>
    <modules>
        <Mage_Shop>
            <version>0.0.1</version>
        </Mage_Shop>
    </modules>
    <global>     
                <models>
                        <mage_shop>
                                <class>Mage_Shop_Model</class>
                                <resourceModel>Shop_mysql4</resourceModel>
                        </mage_shop>
                        <mage_shop_mysql4>
                                <class>Mage_Shop_Model_Mysql4</class>
                                <entities>
                                        <mage_shop>
                                                <table>order_logs</table>
                                        </mage_shop>
                                </entities>
                        </mage_shop_mysql4>
                </models>
                <resources>
                        <mage_shop_setup>
                                <setup>
                                        <module>Mage_Shop</module>
                                </setup>
                                <connection>
                                        <use>core_setup</use>
                                </connection>
                        </mage_shop_setup>
                        <mage_shop_write>
                                <connection>
                                        <use>core_write</use>
                                </connection>
                        </mage_shop_write>
                        <mage_shop_read>
                                <connection>
                                        <use>core_read</use>
                                </connection>
                        </mage_shop_read>
                </resources>  
        <models>
            <mage_shop>
                <class>Mage_Shop_Model</class>
            </mage_shop>
        </models>
                <helpers>
                        <mage_shop>
                                <class>mage_shop_Helper</class>
                        </mage_shop>
                </helpers>
        <events>
           <sales_order_place_after>
                <observers>
                    <mage_shop>
                        <class>mage_shop/observer</class>
                        <method>logUpdate</method>
                        <type>model</type>
                    </mage_shop>
                </observers>
            </sales_order_place_after>        
        </events>
    </global>
</config>

I ignore <frontend> and <block> because I dont want to show nothing in backend or in front.

IndexController.php (Mage/Shop/controllers/IndexController.php)

<?php
class Mage_Shop_IndexController extends Mage_Core_Controller_Front_Action
    {
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}

Shop.php (Mage/Shop/Model/Shop.php)

<?php
class Mage_Shop_Model_Shop extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('shop/order_logs');
    }
}

Shop.php (Mage/Shop/Model/Mysql4/Shop.php)

<?php
class Mage_Shop_Model_Mysql4_Shop extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('shop/logs', 'order_id');
    }
}

Collection.php (Mage/Shop/Model/Mysql4/Shop/Collection.php)

<?php
class Mage_Shop_Model_Mysql4_Shop_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        //parent::__construct();
        $this->_init('shop/order_logs');
    }
}

mysql4-install-0.1.0.php (Mage/Shop/sql/shop_setup/mysql4-install-0.1.0.php)

<?php
$installer = $this;
$installer->startSetup();
$installer->run("
    — DROP TABLE IF EXISTS {$this->getTable('order_logs')};
    CREATE TABLE {$this->getTable('order_logs')} (
          `order_id` int(20),
          `array_log` json,
          PRIMARY KEY (`order_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();

Insert code in Observer.php (Mage/Shop/Model/Observer.php)

$connW = $coreResource->getConnection('core_write');

      $connW->insert()
        ->into($coreResource->getTableName('order_logs'))
        ->columns(array('order_id','array_log'))
        ->values(array('order_id' => $order_id, 
                       'array_log' => $content  //$content is json   
                      )
                );

I followed this: https://magento2.atlassian.net/wiki/spaces/m1wiki/pages/14024903/Create+a+custom+module+with+a+custom+database+table

Best Answer

Please change your observer code as below for enter record in table:

$order_id = "some recoed";
$content = "some content";

$resource = Mage::getSingleton('core/resource');

$writeConnection = $resource->getConnection('core_write');

$query = "INSERT INTO `order_logs` (`order_id`,`array_log`) VALUES ('.$order_id.', '.$content.')";

$writeConnection->query($query);
Related Topic