2013-01-10 32 views
2

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

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

这是我代码\本地\ FS \ ThemeEditor \等\ 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> 

这是我代码\本地\ FS \ ThemeEditor \型号\ Customersite.php

<?php 

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

这是我代码\本地\ FS \ ThemeEditor \型号\ 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'); 
    } 
} 

这是我代码\本地\ FS \ ThemeEditor \型号\ 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'); 
    } 
} 

这是我代码\本地\ FS \ ThemeEditor \ SQL \ themeeditor_setup \ mysql4安装-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(); 

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

回答

0

尝试目前店内ID设置为管理店,然后再储存:

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

设置id时,也有类似的问题,保存不更新,而不是插入的,这意味着保存失败。 – user3145914

1

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

protected $_isPkAutoIncrement = false; 
相关问题