2

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;
}

这是我的代码\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');
    }
}

这是我的代码\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();

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

4

2 回答 2

3

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

protected $_isPkAutoIncrement = false;
于 2016-12-27T05:12:19.987 回答
0

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

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
于 2013-03-01T16:17:12.237 回答