I need to create default billing address for new user. To create billing/shipping address I'm using the next code:

protected function createAddress($isDefaultBilling = false, $isDefaultShipping = false) {

        $address = $this->addressFactory->create()

            return $this->addressRepository->save($address);
            // die('testing'); // wut???
            // return $address;


$this->addressFactory is an instance of \Magento\Customer\Api\Data\AddressInterfaceFactory

and $this->addressRepository is \Magento\Customer\Api\CustomerRepositoryInterface

The code looks properly, and in var/debug/db.log I see the query:

## 2016-10-14 10:52:27
## 2909 ## QUERY
SQL: INSERT INTO `mg_customer_address_entity` (`parent_id`, `created_at`, `updated_at`, `city`, `company`, `country_id`, `fax`, `firstname`, `lastname`, `postcode`, `street`, `telephone`) VALUES (?, '2016-10-14 10:52:27', '2016-10-14 10:52:27', ?, ?, ?, ?, ?, ?, ?, ?, ?)
BIND: array (
  0 => 12,
  1 => 'n/a',
  2 => 'n/a',
  3 => 'UA',
  4 => 'n/a',
  5 => 'fname',
  6 => 'lname',
  7 => 'n/a',
  8 => 'n/a',
  9 => 'n/a',
AFF: 1
TIME: 0.0015

BUT there is NO data in database!

For debug, I saw in /home/serf/www/magento/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php, and in function save the call $this->isModified($object) returns false.

I tried to use \Magento\Customer\Model\AddressFactory:

$model = $this->addressModelFactory->create();
$address = $model->getDataModel()

But the result was not changed. And, finally, I tried $model->getResource()->save($model). And got an error like "City is required field".

I tried more ways, but each time the behaviour is different: it may assign new ID to billing/shipping (but the data was not saved in database!), or die with message like "No such entity with addressId = 122", when I try to edit the new billing.

In the last I've uncommented the string die('testing');, and the address was created. Even if I don't change the billing after creating, it appears in database only if die(..) is uncommented. So, what am I doing wrong?

UPD. How am I using the code:

$this->defaultBilling = $this->createAddress(true, false);

where $this->customerObject is instance of Magento\Customer\Api\Data\CustomerInterface, and $this->customerRepository -- Magento\Customer\Api\CustomerRepositoryInterface

1 Answer 1


Saving customer is not required. On string $this->customerRepository->save($this->customerObject); the transaction (where billing is saving to database) was rolled back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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