Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Customer Class Reference
Inheritance diagram for Customer:
AbstractEntity AbstractEntity AbstractResource EntityInterface ProviderInterface

Public Member Functions

 __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\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Validator\Factory $validatorFactory, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Store\Model\StoreManagerInterface $storeManager, $data=[])
 
 loadByEmail (\Magento\Customer\Model\Customer $customer, $email)
 
 changePassword (\Magento\Customer\Model\Customer $customer, $newPassword)
 
 findEmailDuplicates ()
 
 checkCustomerId ($customerId)
 
 getWebsiteId ($customerId)
 
 setNewIncrementId (\Magento\Framework\DataObject $object)
 
 changeResetPasswordLinkToken (\Magento\Customer\Model\Customer $customer, $passwordLinkToken)
 
- Public Member Functions inherited from AbstractEntity
 __construct (\Magento\Eav\Model\Entity\Context $context, \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite, $data=[])
 
 save (\Magento\Framework\Model\AbstractModel $object)
 
- Public Member Functions inherited from AbstractEntity
 __construct (Context $context, $data=[])
 
 setConnection ($connection)
 
 getConnection ()
 
 getIdFieldName ()
 
 getTable ($alias)
 
 setType ($type)
 
 getEntityType ()
 
 getType ()
 
 getTypeId ()
 
 unsetAttributes ($attributes=null)
 
 addAttribute (AbstractAttribute $attribute, $object=null)
 
 isPartialLoad ($flag=null)
 
 isPartialSave ($flag=null)
 
 loadAllAttributes ($object=null)
 
 getSortedAttributes ($setId=null)
 
 attributesCompare ($firstAttribute, $secondAttribute)
 
 getAttributesByCode ()
 
 getAttributesByTable ()
 
 getEntityTable ()
 
 getLinkField ()
 
 getEntityIdField ()
 
 getValueEntityIdField ()
 
 getValueTablePrefix ()
 
 getEntityTablePrefix ()
 
 isAttributeStatic ($attribute)
 
 validate ($object)
 
 setNewIncrementId (DataObject $object)
 
 checkAttributeUniqueValue (AbstractAttribute $attribute, $object)
 
 getDefaultAttributeSourceModel ()
 
 load ($object, $entityId, $attributes=[])
 
 save (\Magento\Framework\Model\AbstractModel $object)
 
 saveAttribute (DataObject $object, $attributeCode)
 
 delete ($object)
 
 getDefaultAttributes ()
 
 afterLoad (DataObject $object)
 
 beforeSave (DataObject $object)
 
 afterSave (DataObject $object)
 
 beforeDelete (DataObject $object)
 
 afterDelete (DataObject $object)
 
- Public Member Functions inherited from AbstractResource
 __construct ()
 
 getConnection ()
 
 beginTransaction ()
 
 addCommitCallback ($callback)
 
 commit ()
 
 rollBack ()
 
 getValidationRulesBeforeSave ()
 

Protected Member Functions

 _getDefaultAttributes ()
 
 _validate ($customer)
 
 _afterSave (\Magento\Framework\DataObject $customer)
 
 _getLoadRowSelect ($object, $rowId)
 
- Protected Member Functions inherited from AbstractEntity
 _afterLoad (\Magento\Framework\DataObject $object)
 
 isModified (\Magento\Framework\Model\AbstractModel $object)
 
- Protected Member Functions inherited from AbstractEntity
 _construct ()
 
 _getConfig ()
 
 _isApplicableAttribute ($object, $attribute)
 
 _isCallableAttributeInstance ($instance, $method, $args)
 
 loadAttributesMetadata ($attributes)
 
 _loadModelAttributes ($object)
 
 _prepareLoadSelect (array $selects)
 
 _getLoadRowSelect ($object, $rowId)
 
 _getLoadAttributesSelect ($object, $table)
 
 _setAttributeValue ($object, $valueRow)
 
 processSave ($object)
 
 _getOrigObject ($object)
 
 _collectSaveData ($newObject)
 
 _canUpdateAttribute (AbstractAttribute $attribute, $v, array &$origData)
 
 _getStaticFieldProperties ($field)
 
 _prepareStaticValue ($key, $value)
 
 _insertAttribute ($object, $attribute, $value)
 
 _updateAttribute ($object, $attribute, $valueId, $value)
 
 _saveAttribute ($object, $attribute, $value)
 
 _processAttributeValues ()
 
 _prepareValueForSave ($value, AbstractAttribute $attribute)
 
 _deleteAttributes ($object, $table, $info)
 
 getAttributeRow ($entity, $object, $attribute)
 
 evaluateDelete ($object, $id, $connection)
 
 _afterLoad (DataObject $object)
 
 _beforeSave (DataObject $object)
 
 _afterSave (DataObject $object)
 
 _beforeDelete (DataObject $object)
 
 _afterDelete (DataObject $object)
 
 _getDefaultAttributeModel ()
 
 _getDefaultAttributes ()
 
 _isAttributeValueEmpty (AbstractAttribute $attribute, $value)
 
 getAttributeLoader ()
 
 loadAttributesForObject ($attributes, $object=null)
 
- Protected Member Functions inherited from AbstractResource
 _construct ()
 
 _serializeField (DataObject $object, $field, $defaultValue=null, $unsetEmpty=false)
 
 _unserializeField (DataObject $object, $field, $defaultValue=null)
 
 _prepareDataForTable (DataObject $object, $table)
 
 _prepareTableValueForSave ($value, $type)
 
 _getColumnsForEntityLoad (\Magento\Framework\Model\AbstractModel $object, $tableName)
 
 getSerializer ()
 

Protected Attributes

 $_validatorFactory
 
 $_scopeConfig
 
 $dateTime
 
 $storeManager
 
- Protected Attributes inherited from AbstractEntity
 $entitySnapshot
 
 $entityRelationComposite
 
- Protected Attributes inherited from AbstractEntity
 $attributeLoader
 
 $connectionName
 
 $_type
 
 $_attributesByCode = []
 
 $_attributesByTable = []
 
 $_staticAttributes = []
 
 $_entityTable
 
 $_describeTable = []
 
 $_entityIdField
 
 $linkIdField
 
 $_valueEntityIdField
 
 $_valueTablePrefix
 
 $_entityTablePrefix
 
 $_isPartialLoad = false
 
 $_isPartialSave = false
 
 $_sortingSetId = null
 
 $_attributeValuesToDelete = []
 
 $_attributeValuesToSave = []
 
 $_resource
 
 $_eavConfig
 
 $_attrSetEntity
 
 $_localeFormat
 
 $_resourceHelper
 
 $_universalFactory
 
 $transactionManager
 
 $objectRelationProcessor
 
- Protected Attributes inherited from AbstractResource
 $serializer
 
 $_logger
 

Additional Inherited Members

- Static Protected Attributes inherited from AbstractEntity
static $_attributeBackendTables = []
 

Detailed Description

Customer entity resource model

@api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 21 of file Customer.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Eav\Model\Entity\Context$context
\Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot$entitySnapshot
\Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite$entityRelationComposite
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\Validator\Factory$validatorFactory
\Magento\Framework\Stdlib\DateTime$dateTime
\Magento\Store\Model\StoreManagerInterface$storeManager
array$data

Definition at line 60 of file Customer.php.

69  {
70  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data);
71  $this->_scopeConfig = $scopeConfig;
72  $this->_validatorFactory = $validatorFactory;
73  $this->dateTime = $dateTime;
74  $this->storeManager = $storeManager;
75  $this->setType('customer');
76  $this->setConnection('customer_read', 'customer_write');
77  }

Member Function Documentation

◆ _afterSave()

_afterSave ( \Magento\Framework\DataObject  $customer)
protected

Save customer addresses and set default addresses in attributes backend

Parameters
\Magento\Framework\DataObject$customer
Returns
$this

Definition at line 198 of file Customer.php.

199  {
200  $this->getNotificationStorage()->add(
202  $customer->getId()
203  );
204  return parent::_afterSave($customer);
205  }
$customer
Definition: customers.php:11

◆ _getDefaultAttributes()

_getDefaultAttributes ( )
protected

Retrieve customer entity default attributes

Returns
string[]

Definition at line 84 of file Customer.php.

85  {
86  return [
87  'created_at',
88  'updated_at',
89  'increment_id',
90  'store_id',
91  'website_id'
92  ];
93  }

◆ _getLoadRowSelect()

_getLoadRowSelect (   $object,
  $rowId 
)
protected

Retrieve select object for loading base entity row

Parameters
\Magento\Framework\DataObject$object
string | int$rowId
Returns
\Magento\Framework\DB\Select

Definition at line 214 of file Customer.php.

215  {
216  $select = parent::_getLoadRowSelect($object, $rowId);
217  if ($object->getWebsiteId() && $object->getSharingConfig()->isWebsiteScope()) {
218  $select->where('website_id =?', (int)$object->getWebsiteId());
219  }
220 
221  return $select;
222  }

◆ _validate()

_validate (   $customer)
protected

Validate customer entity

Parameters
\Magento\Customer\Model\Customer$customer
Returns
void
Exceptions

Definition at line 166 of file Customer.php.

167  {
168  $validator = $this->_validatorFactory->createValidator('customer', 'save');
169 
170  if (!$validator->isValid($customer)) {
171  throw new ValidatorException(
172  null,
173  null,
174  $validator->getMessages()
175  );
176  }
177  }
$customer
Definition: customers.php:11

◆ changePassword()

changePassword ( \Magento\Customer\Model\Customer  $customer,
  $newPassword 
)

Change customer password

Parameters
\Magento\Customer\Model\Customer$customer
string$newPassword
Returns
$this

Definition at line 270 of file Customer.php.

271  {
272  $customer->setPassword($newPassword);
273  return $this;
274  }
$customer
Definition: customers.php:11

◆ changeResetPasswordLinkToken()

changeResetPasswordLinkToken ( \Magento\Customer\Model\Customer  $customer,
  $passwordLinkToken 
)

Change reset password link token

Stores new reset password link token and its creation time

Parameters
\Magento\Customer\Model\Customer$customer
string$passwordLinkToken
Returns
$this

Definition at line 374 of file Customer.php.

375  {
376  if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
377  $customer->setRpToken($passwordLinkToken);
378  $customer->setRpTokenCreatedAt(
379  (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
380  );
381  }
382  return $this;
383  }
$customer
Definition: customers.php:11

◆ checkCustomerId()

checkCustomerId (   $customerId)

Check customer by id

Parameters
int$customerId
Returns
bool

Definition at line 307 of file Customer.php.

308  {
309  $connection = $this->getConnection();
310  $bind = ['entity_id' => (int)$customerId];
311  $select = $connection->select()->from(
312  $this->getTable('customer_entity'),
313  'entity_id'
314  )->where(
315  'entity_id = :entity_id'
316  )->limit(
317  1
318  );
319 
320  $result = $connection->fetchOne($select, $bind);
321  if ($result) {
322  return true;
323  }
324  return false;
325  }
$connection
Definition: bulk.php:13

◆ findEmailDuplicates()

findEmailDuplicates ( )

Check whether there are email duplicates of customers in global scope

Returns
bool

Definition at line 281 of file Customer.php.

282  {
283  $connection = $this->getConnection();
284  $select = $connection->select()->from(
285  $this->getTable('customer_entity'),
286  ['email', 'cnt' => 'COUNT(*)']
287  )->group(
288  'email'
289  )->order(
290  'cnt DESC'
291  )->limit(
292  1
293  );
294  $lookup = $connection->fetchRow($select);
295  if (empty($lookup)) {
296  return false;
297  }
298  return $lookup['cnt'] > 1;
299  }
$connection
Definition: bulk.php:13

◆ getWebsiteId()

getWebsiteId (   $customerId)

Get customer website id

Parameters
int$customerId
Returns
int

Definition at line 333 of file Customer.php.

334  {
335  $connection = $this->getConnection();
336  $bind = ['entity_id' => (int)$customerId];
337  $select = $connection->select()->from(
338  $this->getTable('customer_entity'),
339  'website_id'
340  )->where(
341  'entity_id = :entity_id'
342  );
343 
344  return $connection->fetchOne($select, $bind);
345  }
$connection
Definition: bulk.php:13

◆ loadByEmail()

loadByEmail ( \Magento\Customer\Model\Customer  $customer,
  $email 
)

Load customer by email

Parameters
\Magento\Customer\Model\Customer$customer
string$email
Returns
$this
Exceptions

Definition at line 232 of file Customer.php.

233  {
234  $connection = $this->getConnection();
235  $bind = ['customer_email' => $email];
236  $select = $connection->select()->from(
237  $this->getEntityTable(),
238  [$this->getEntityIdField()]
239  )->where(
240  'email = :customer_email'
241  );
242 
243  if ($customer->getSharingConfig()->isWebsiteScope()) {
244  if (!$customer->hasData('website_id')) {
245  throw new \Magento\Framework\Exception\LocalizedException(
246  __("A customer website ID wasn't specified. The ID must be specified to use the website scope.")
247  );
248  }
249  $bind['website_id'] = (int)$customer->getWebsiteId();
250  $select->where('website_id = :website_id');
251  }
252 
253  $customerId = $connection->fetchOne($select, $bind);
254  if ($customerId) {
255  $this->load($customer, $customerId);
256  } else {
257  $customer->setData([]);
258  }
259 
260  return $this;
261  }
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
load($object, $entityId, $attributes=[])
__()
Definition: __.php:13
$connection
Definition: bulk.php:13

◆ setNewIncrementId()

setNewIncrementId ( \Magento\Framework\DataObject  $object)

Custom setter of increment ID if its needed

Parameters
\Magento\Framework\DataObject$object
Returns
$this

Definition at line 353 of file Customer.php.

354  {
355  if ($this->_scopeConfig->getValue(
357  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
358  )
359  ) {
360  parent::setNewIncrementId($object);
361  }
362  return $this;
363  }

Field Documentation

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 33 of file Customer.php.

◆ $_validatorFactory

$_validatorFactory
protected

Definition at line 26 of file Customer.php.

◆ $dateTime

$dateTime
protected

Definition at line 38 of file Customer.php.

◆ $storeManager

$storeManager
protected

Definition at line 43 of file Customer.php.


The documentation for this class was generated from the following file: