Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Address.php
Go to the documentation of this file.
1 <?php
9 
14 
20 class Address extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
21 {
25  protected $_validatorFactory;
26 
31 
40  public function __construct(
41  \Magento\Eav\Model\Entity\Context $context,
42  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
43  \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite,
44  \Magento\Framework\Validator\Factory $validatorFactory,
46  $data = []
47  ) {
48  $this->customerRepository = $customerRepository;
49  $this->_validatorFactory = $validatorFactory;
50  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data);
51  }
52 
58  protected function _construct()
59  {
60  $this->connectionName = 'customer';
61  }
62 
69  public function getEntityType()
70  {
71  if (empty($this->_type)) {
72  $this->setType('customer_address');
73  }
74  return parent::getEntityType();
75  }
76 
83  protected function _beforeSave(\Magento\Framework\DataObject $address)
84  {
85  parent::_beforeSave($address);
86 
87  $this->_validate($address);
88 
89  return $this;
90  }
91 
99  protected function _validate($address)
100  {
101  $validator = $this->_validatorFactory->createValidator('customer_address', 'save');
102 
103  if (!$validator->isValid($address)) {
104  throw new \Magento\Framework\Validator\Exception(
105  null,
106  null,
107  $validator->getMessages()
108  );
109  }
110  }
111 
115  public function delete($object)
116  {
117  $result = parent::delete($object);
118  $object->setData([]);
119  return $result;
120  }
121 
126  private function getDeleteRelation()
127  {
128  return ObjectManager::getInstance()->get(DeleteRelation::class);
129  }
130 
135  private function getCustomerRegistry()
136  {
137  return ObjectManager::getInstance()->get(CustomerRegistry::class);
138  }
139 
144  protected function _afterDelete(\Magento\Framework\DataObject $address)
145  {
146  $customer = $this->getCustomerRegistry()->retrieve($address->getCustomerId());
147 
148  $this->getDeleteRelation()->deleteRelation($address, $customer);
149  return parent::_afterDelete($address);
150  }
151 }
$customer
Definition: customers.php:11
_beforeSave(\Magento\Framework\DataObject $address)
Definition: Address.php:83
$address
Definition: customer.php:38
_afterDelete(\Magento\Framework\DataObject $address)
Definition: Address.php:144
__construct(\Magento\Eav\Model\Entity\Context $context, \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite, \Magento\Framework\Validator\Factory $validatorFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, $data=[])
Definition: Address.php:40