更新客户的地址在Observer.php中工作,在自定义PHP文件中失败

时间:2014-06-27 09:04:41

标签: php sql ajax magento

我已在Checkout中更改客户地址时将AJAX调用连接到事件。它应该更新客户地址中的1个字段。

require('../../../../Mage.php');
umask(0);
Mage::app();

$newContactologyClientId = $_POST['contactologyClientId'];

$customerDefaultAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
$customerAddress = Mage::getModel('customer/address');
$customerAddress = $customerAddress->load($customerDefaultAddressId); 

$_new_address = array (
    'contactology_client_id' => "$newContactologyClientId"
    );

$customerAddress->addData($_new_address);
$customerAddress->implodeStreetAddress()->save();

在JavaScript方面一切正常,所以我不会在这里包含它。负责数据库更新的已发布代码在我的模块的Magento事件(sales_quote_save_after)上的Observer.php中启动时有效,但是当我通过AJAX运行时,我收到错误:

Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: 
Integrity constraint violation: 1452 Cannot add or update a child row: 
a foreign key constraint fails (`mag6`.`customer_address_entity`, 
CONSTRAINT `FK_CUSTOMER_ADDRESS_ENTITY_PARENT_ID_CUSTOMER_ENTITY_ENTITY_ID` 
FOREIGN KEY (`parent_id`) REFERENCES `customer_entity` (`entity_id`) 
ON DELETE CAS)' in C:\(...)\Pdo.php:228

导致错误的行自然是最后一行:

$customerAddress->implodeStreetAddress()->save();

所以我认为如果数据库操作在Observer.php中有效并且不在我的自定义onCheckoutAddressEdit.php中,那是因为包括Mage:app()是不够的,我需要额外的课程,但不知道哪一个。

1 个答案:

答案 0 :(得分:1)

我会说你错过了:

Mage::run($mageRunCode, $mageRunType);

查看代码,看起来$customerDefaultAddressId返回NULL,因此客户会话不能正确实例化。