1

我编写了代码来为客户创建或更新地址。创建地址按预期工作,但是更新地址给了我以下 SQL 错误:

无法设置客户地址信息 - SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(magedev. 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)

这是我的代码:

// Determine Address Type
$is_default_billing  = 0;
$is_default_shipping = 0;
$addressType = intval($address_info->AddressType);
if ($addressType >= 0) {
    if ($addressType == 0) {
        $is_default_billing  = 1;
        $is_default_shipping = 1;
    } else if ($addressType == 1) {
        $is_default_billing  = 1;
    } else if ($addressType == 2) {
        $is_default_shipping = 1;
    }
}

// Parse Address Data
$address_data = array(
    'firstname'           => $address_info->Firstname,
    'lastname'            => $address_info->Lastname,
    'company'             => $address_info->Company,
    'street'              => array(
        0 => $address_info->AddressLine1,
        1 => $address_info->AddressLine2
    ),
    'city'                => $address_info->City,
    'region_id'           => '',
    'region'              => $address_info->Region,
    'postcode'            => $address_info->Postcode,
    'country_id'          => getCountryId($address_info->Country),
    'telephone'           => $address_info->Telephone,
    'is_default_billing'  => $is_default_billing,
    'is_default_shipping' => $is_default_shipping
);

// Anticipate Error
try
{
    // Get Customer Model
    $customAddress = Mage::getModel('customer/address');

    // Address Create Mode
    if ($address_info->QueueAction == 'Create')
    {
        // Create Address
        $customAddress->setData($address_data)
                      ->setCustomerId(intval($address_info->MagentoCustomerId))
                      ->setSaveInAddressBook(true)
                      ->save();
    }

    // Address Update Mode
    else if ($address_info->QueueAction == 'Update')
    {
        // Update Address
        $customAddress->load(intval($address_info->MagentoId));
        $customAddress->setData($address_data)
                      ->save();
    }

    // Resource Clean-Up
    $customAddress = null;

    // Success - Returns Queue Id Back
    API_Response(false, '', $address_info->QueueId);
}
catch (Exception $e)
{
    // Error
    API_Response(true, $e->getMessage());
}

我在这里做错了什么?

4

1 回答 1

1

很可能您正在尝试为不存在的客户设置地址。
尝试这个:

->setParentId(intval($address_info->MagentoCustomerId))

代替

->setCustomerId(intval($address_info->MagentoCustomerId))
于 2013-09-13T17:27:13.560 回答