0

I want to insert some information of customer into all customer tables from outside form but I am not able to insert it and I have also used following code :

$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
    ->setFirstname($customer->getFirstname())
    ->setMiddleName($customer->getMiddlename())
    ->setLastname($customer->getLastname())
    ->setCountryId('US')
    //->setRegionId('1') //state/province, only needed if the country is USA
    ->setPostcode('90210')
    ->setCity('Beverly Hills')
    ->setTelephone('123')
    ->setFax('123')
    ->setCompany('Company')
    ->setStreet('Street')
    ->setIsDefaultBilling('1')
    ->setIsDefaultShipping('1')
    ->setSaveInAddressBook('1');

try{
    $address->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

But I am getting some error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`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 )' in C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php:228
Stack trace:
#0 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php(228): PDOStatement->execute(Array)
#1 C:\wamp\www\magento\mmm\magento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) 
#2 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array) 
#3 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array) 
#4 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `cu...', Array) in C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php on line 234

Please help me and suggest me how to solve it so that I can insert some information of customer into database from outside form. Thanks

  • please check customer id exit in system and also check customer website – Amit Bera Feb 24 '15 at 9:43
  • Your problem is, that you want to save a customer address without customer_id – Fabian Blechschmidt Feb 24 '15 at 11:30
1

I have solved this problem and I used following code :

require PATH_TO_MAGENTO_FOLDER . '/app/Mage.php';
Mage::app();

$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");  //If you want to insert only customer information into DB then use it.
$customer   ->setWebsiteId($websiteId)
        ->setStore($store)
        ->setStoreId(0)
        ->setGroupId(1) // if you want to change group then used GroupId 1 or 2 or 3 
        ->setPrefix('Sir')
        ->setFirstname('john')
        ->setMiddleName('2')
        ->setLastname('Doe')
        ->setSuffix('II')
        ->setEmail('abc@gmail.com')
        ->setPassword('password')
        ->save();

$address = Mage::getModel("customer/address"); // If you also want to insert all address into DB then use it.
$address->setCustomerId($customer->getId())
    ->setFirstname($customer->getFirstname())
    ->setMiddleName($customer->getMiddlename())
    ->setLastname($customer->getLastname())
    ->setCountryId('IN')
    ->setRegionId('1') //state/province, only needed if the country is USA
    ->setRegion('MP')
    ->setPostcode('31000')
    ->setCity('Osijek')
    ->setTelephone('0038511223344')
    ->setFax('0038511223355')
    ->setCompany('Inchoo')
    ->setStreet('Kersov')
    ->setIsDefaultBilling('0')
    ->setIsDefaultShipping('1')
    ->setSaveInAddressBook('1')
    ->save();                   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.