Magento:object-> save()不会插入数据库

时间:2013-01-10 18:18:50

标签: magento-1.7

model->save()未插入数据库。 Magento缓存关闭。
我没有收到任何问题,没关系,但它没有插入数据库 我的代码如下:

try
{
    $cusSite = Mage::getModel('themeeditor/customersite');
                $cusSite->setData(array("customer_id" => 1,
                              "website_id" => 1,
                              "store_group" => 1,
                              "store_id" => 1));            
                $query = $cusSite->save();
}
catch(Exception $e)
{
    echo $e->getMessage;exit;
}

这是我的 code \ local \ FS \ ThemeEditor \ etc \ config.xml

   <models>
                <themeeditor>
                    <class>FS_ThemeEditor_Model</class>
                    <resourceModel>themeeditor_mysql4</resourceModel>
                </themeeditor>
                <themeeditor_mysql4>
                    <class>FS_ThemeEditor_Model_Mysql4</class>
                    <entities>
                        <customersite>
                            <table>themeeditor_customersite</table>
                        </customersite>
                    </entities>
                </themeeditor_mysql4>
            </models>
<resources>             
            <themeeditor_setup>
                <setup>
                    <module>FS_ThemeEditor</module>
                    <class>Mage_catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </themeeditor_setup>
            <themeeditor_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </themeeditor_write>
            <themeeditor_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </themeeditor_read>
        </resources>

这是我的代码\ local \ FS \ ThemeEditor \ Model \ Customersite.php

<?php

class FS_ThemeEditor_Model_Customersite extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('themeeditor/customersite');
    }
}

这是我的代码\ local \ FS \ ThemeEditor \ Model \ Mysql4 \ Customersite.php

<?php

class FS_ThemeEditor_Model_Mysql4_Customersite extends Mage_Core_Model_Mysql4_Abstract
{   
    public function _construct()
    {    
        // Note that the billingaddress_id refers to the key field in your database table.
        $this->_init('themeeditor/customersite', 'customer_id');
    }
}

这是我的代码\ local \ FS \ ThemeEditor \ Model \ Mysql4 \ Customersite \ Collection.php

<?php

class FS_ThemeEditor_Model_Mysql4_Customersite_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('themeeditor/customersite');
    }
}

这是我的 code \ local \ FS \ ThemeEditor \ sql \ themeeditor_setup \ mysql4-install-0.1.0.php

<?php 
$installer = $this;

$installer->startSetup();

$installer->run("
DROP TABLE IF EXISTS {$this->getTable('themeeditor_customersite')};
CREATE TABLE {$this->getTable('themeeditor_customersite')}(
    `customer_id` int(10) unsigned NOT NULL,
    `website_id` int(10) unsigned NOT NULL,
    `store_id` int(10) unsigned NOT NULL,
    `store_group` int(10) unsigned NOT NULL,
    FOREIGN KEY (`customer_id`) REFERENCES {$this->getTable('customer_entity')} (`entity_id`) ,
    PRIMARY KEY (`customer_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;");

$installer->endSetup();

我不知道它无法插入数据库的原因。

2 个答案:

答案 0 :(得分:2)

我有类似的问题。因为我的主键不是自动生成的,所以Magento看到Id字段有一个值,并假设保存是更新而不是插入。修复是将以下行添加到Model_Resource类:

protected $_isPkAutoIncrement = false;

答案 1 :(得分:0)

在保存之前,请尝试将当前商店ID设置为管理存储:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);