Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
AbstractModel Class Reference
Inheritance diagram for AbstractModel:
AbstractExtensibleModel AbstractModel CustomAttributesDataInterface DataObject ExtensibleDataInterface Rule Rule Rule

Public Member Functions

 getConditionsInstance ()
 
 getActionsInstance ()
 
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], ExtensionAttributesFactory $extensionFactory=null, AttributeValueFactory $customAttributeFactory=null, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
 
 beforeSave ()
 
 setConditions ($conditions)
 
 getConditions ()
 
 setActions ($actions)
 
 getActions ()
 
 getForm ()
 
 loadPost (array $data)
 
 validate (\Magento\Framework\DataObject $object)
 
 validateData (\Magento\Framework\DataObject $dataObject)
 
 isDeleteable ()
 
 setIsDeleteable ($value)
 
 isReadonly ()
 
 setIsReadonly ($value)
 
 getWebsiteIds ()
 
- Public Member Functions inherited from AbstractExtensibleModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 getCustomAttributes ()
 
 getCustomAttribute ($attributeCode)
 
 setCustomAttributes (array $attributes)
 
 setCustomAttribute ($attributeCode, $attributeValue)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 setId ($value)
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Member Functions

 _resetConditions ($conditions=null)
 
 _resetActions ($actions=null)
 
 _convertFlatToRecursive (array $data)
 
- Protected Member Functions inherited from AbstractExtensibleModel
 filterCustomAttributes ($data)
 
 initializeCustomAttributes ()
 
 convertCustomAttributeValues (array &$customAttributes)
 
 getCustomAttributesCodes ()
 
 _setExtensionAttributes (\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
 
 _getExtensionAttributes ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_conditions
 
 $_actions
 
 $_form
 
 $_isDeleteable = true
 
 $_isReadonly = false
 
 $serializer
 
 $_formFactory
 
 $_localeDate
 
- Protected Attributes inherited from AbstractExtensibleModel
 $extensionAttributesFactory
 
 $extensionAttributes
 
 $customAttributeFactory
 
 $customAttributesCodes = null
 
 $customAttributesChanged = false
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Data Fields inherited from CustomAttributesDataInterface
const CUSTOM_ATTRIBUTES = 'custom_attributes'
 
- Data Fields inherited from ExtensibleDataInterface
const EXTENSION_ATTRIBUTES_KEY = 'extension_attributes'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Abstract Rule entity data model

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @api

Since
100.0.2

Definition at line 18 of file AbstractModel.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\Context  $context,
\Magento\Framework\Registry  $registry,
\Magento\Framework\Data\FormFactory  $formFactory,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface  $localeDate,
\Magento\Framework\Model\ResourceModel\AbstractResource  $resource = null,
\Magento\Framework\Data\Collection\AbstractDb  $resourceCollection = null,
array  $data = [],
ExtensionAttributesFactory  $extensionFactory = null,
AttributeValueFactory  $customAttributeFactory = null,
\Magento\Framework\Serialize\Serializer\Json  $serializer = null 
)

AbstractModel constructor

Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Framework\Data\FormFactory$formFactory
\Magento\Framework\Stdlib\DateTime\TimezoneInterface$localeDate
\Magento\Framework\Model\ResourceModel\AbstractResource | null$resource
\Magento\Framework\Data\Collection\AbstractDb | null$resourceCollection
array$data
ExtensionAttributesFactory | null$extensionFactory
AttributeValueFactory | null$customAttributeFactory
\Magento\Framework\Serialize\Serializer\Json$serializer@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 104 of file AbstractModel.php.

115  {
116  $this->_formFactory = $formFactory;
117  $this->_localeDate = $localeDate;
119  \Magento\Framework\Serialize\Serializer\Json::class
120  );
121  parent::__construct(
122  $context,
123  $registry,
124  $extensionFactory ?: $this->getExtensionFactory(),
125  $customAttributeFactory ?: $this->getCustomAttributeFactory(),
126  $resource,
127  $resourceCollection,
128  $data
129  );
130  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _convertFlatToRecursive()

_convertFlatToRecursive ( array  $data)
protected

Set specified data to current rule. Set conditions and actions recursively. Convert dates into \DateTime.

Parameters
array$data
Returns
array @SuppressWarnings(PHPMD.CyclomaticComplexity)

Convert dates into \DateTime

Definition at line 337 of file AbstractModel.php.

338  {
339  $arr = [];
340  foreach ($data as $key => $value) {
341  if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
342  foreach ($value as $id => $data) {
343  $path = explode('--', $id);
344  $node = & $arr;
345  for ($i = 0, $l = sizeof($path); $i < $l; $i++) {
346  if (!isset($node[$key][$path[$i]])) {
347  $node[$key][$path[$i]] = [];
348  }
349  $node = & $node[$key][$path[$i]];
350  }
351  foreach ($data as $k => $v) {
352  $node[$k] = $v;
353  }
354  }
355  } else {
359  if (in_array($key, ['from_date', 'to_date'], true) && $value) {
360  $value = new \DateTime($value);
361  }
362  $this->setData($key, $value);
363  }
364  }
365 
366  return $arr;
367  }
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16
if(isset($opts->o)) if(! $usingStdout) $l
$i
Definition: gallery.phtml:31

◆ _resetActions()

_resetActions (   $actions = null)
protected

Reset rule actions

Parameters
null | \Magento\Rule\Model\Action\Collection$actions
Returns
$this

Definition at line 285 of file AbstractModel.php.

286  {
287  if (null === $actions) {
288  $actions = $this->getActionsInstance();
289  }
290  $actions->setRule($this)->setId('1')->setPrefix('actions');
291  $this->setActions($actions);
292 
293  return $this;
294  }

◆ _resetConditions()

_resetConditions (   $conditions = null)
protected

Reset rule combine conditions

Parameters
null | \Magento\Rule\Model\Condition\Combine$conditions
Returns
$this

Definition at line 268 of file AbstractModel.php.

269  {
270  if (null === $conditions) {
271  $conditions = $this->getConditionsInstance();
272  }
273  $conditions->setRule($this)->setId('1')->setPrefix('conditions');
274  $this->setConditions($conditions);
275 
276  return $this;
277  }

◆ beforeSave()

beforeSave ( )

Prepare data before saving

Returns
$this
Exceptions

Prepare website Ids if applicable and if they were set as string in comma separated format. Backwards compatibility.

Prepare customer group Ids if applicable and if they were set as string in comma separated format. Backwards compatibility.

Definition at line 139 of file AbstractModel.php.

140  {
141  // Check if discount amount not negative
142  if ($this->hasDiscountAmount()) {
143  if ((int)$this->getDiscountAmount() < 0) {
144  throw new \Magento\Framework\Exception\LocalizedException(__('Please choose a valid discount amount.'));
145  }
146  }
147 
148  // Serialize conditions
149  if ($this->getConditions()) {
150  $this->setConditionsSerialized($this->serializer->serialize($this->getConditions()->asArray()));
151  $this->_conditions = null;
152  }
153 
154  // Serialize actions
155  if ($this->getActions()) {
156  $this->setActionsSerialized($this->serializer->serialize($this->getActions()->asArray()));
157  $this->_actions = null;
158  }
159 
164  if ($this->hasWebsiteIds()) {
165  $websiteIds = $this->getWebsiteIds();
166  if (is_string($websiteIds) && !empty($websiteIds)) {
167  $this->setWebsiteIds(explode(',', $websiteIds));
168  }
169  }
170 
175  if ($this->hasCustomerGroupIds()) {
176  $groupIds = $this->getCustomerGroupIds();
177  if (is_string($groupIds) && !empty($groupIds)) {
178  $this->setCustomerGroupIds(explode(',', $groupIds));
179  }
180  }
181 
182  parent::beforeSave();
183  return $this;
184  }
__()
Definition: __.php:13

◆ getActions()

getActions ( )

Retrieve rule actions model

Returns
\Magento\Rule\Model\Action\Collection

Definition at line 241 of file AbstractModel.php.

242  {
243  if (!$this->_actions) {
244  $this->_resetActions();
245  }
246 
247  // Load rule actions if it is applicable
248  if ($this->hasActionsSerialized()) {
249  $actions = $this->getActionsSerialized();
250  if (!empty($actions)) {
251  $actions = $this->serializer->unserialize($actions);
252  if (is_array($actions) && !empty($actions)) {
253  $this->_actions->loadArray($actions);
254  }
255  }
256  $this->unsActionsSerialized();
257  }
258 
259  return $this->_actions;
260  }

◆ getActionsInstance()

getActionsInstance ( )
abstract

Getter for rule actions collection instance

Returns
\Magento\Rule\Model\Action\Collection

◆ getConditions()

getConditions ( )

Retrieve rule combine conditions model

Returns
\Magento\Rule\Model\Condition\Combine

Definition at line 203 of file AbstractModel.php.

204  {
205  if (empty($this->_conditions)) {
206  $this->_resetConditions();
207  }
208 
209  // Load rule conditions if it is applicable
210  if ($this->hasConditionsSerialized()) {
211  $conditions = $this->getConditionsSerialized();
212  if (!empty($conditions)) {
213  $conditions = $this->serializer->unserialize($conditions);
214  if (is_array($conditions) && !empty($conditions)) {
215  $this->_conditions->loadArray($conditions);
216  }
217  }
218  $this->unsConditionsSerialized();
219  }
220 
221  return $this->_conditions;
222  }

◆ getConditionsInstance()

getConditionsInstance ( )
abstract

Getter for rule combine conditions instance

Returns
\Magento\Rule\Model\Condition\Combine

◆ getForm()

getForm ( )

Rule form getter

Returns
\Magento\Framework\Data\Form

Definition at line 301 of file AbstractModel.php.

302  {
303  if (!$this->_form) {
304  $this->_form = $this->_formFactory->create();
305  }
306  return $this->_form;
307  }

◆ getWebsiteIds()

getWebsiteIds ( )

Get rule associated website Ids

Returns
array

Definition at line 476 of file AbstractModel.php.

477  {
478  if (!$this->hasWebsiteIds()) {
479  $websiteIds = $this->_getResource()->getWebsiteIds($this->getId());
480  $this->setData('website_ids', (array)$websiteIds);
481  }
482  return $this->_getData('website_ids');
483  }

◆ isDeleteable()

isDeleteable ( )

Check availability to delete rule

Returns
bool @codeCoverageIgnore

Definition at line 429 of file AbstractModel.php.

430  {
431  return $this->_isDeleteable;
432  }

◆ isReadonly()

isReadonly ( )

Check if rule is readonly

Returns
bool @codeCoverageIgnore

Definition at line 453 of file AbstractModel.php.

454  {
455  return $this->_isReadonly;
456  }

◆ loadPost()

loadPost ( array  $data)

Initialize rule model data from array

Parameters
array$data
Returns
$this

Definition at line 315 of file AbstractModel.php.

316  {
317  $arr = $this->_convertFlatToRecursive($data);
318  if (isset($arr['conditions'])) {
319  $this->getConditions()->setConditions([])->loadArray($arr['conditions'][1]);
320  }
321  if (isset($arr['actions'])) {
322  $this->getActions()->setActions([])->loadArray($arr['actions'][1], 'actions');
323  }
324 
325  return $this;
326  }

◆ setActions()

setActions (   $actions)

Set rule actions model

Parameters
\Magento\Rule\Model\Action\Collection$actions
Returns
$this

Definition at line 230 of file AbstractModel.php.

231  {
232  $this->_actions = $actions;
233  return $this;
234  }

◆ setConditions()

setConditions (   $conditions)

Set rule combine conditions model

Parameters
\Magento\Rule\Model\Condition\Combine$conditions
Returns
$this

Definition at line 192 of file AbstractModel.php.

193  {
194  $this->_conditions = $conditions;
195  return $this;
196  }

◆ setIsDeleteable()

setIsDeleteable (   $value)

Set is rule can be deleted flag

Parameters
bool$value
Returns
$this @codeCoverageIgnore

Definition at line 441 of file AbstractModel.php.

442  {
443  $this->_isDeleteable = (bool)$value;
444  return $this;
445  }
$value
Definition: gender.phtml:16

◆ setIsReadonly()

setIsReadonly (   $value)

Set is readonly flag to rule

Parameters
bool$value
Returns
$this @codeCoverageIgnore

Definition at line 465 of file AbstractModel.php.

466  {
467  $this->_isReadonly = (bool)$value;
468  return $this;
469  }
$value
Definition: gender.phtml:16

◆ validate()

validate ( \Magento\Framework\DataObject  $object)

Validate rule conditions to determine if rule can run

Parameters
\Magento\Framework\DataObject$object
Returns
bool

Definition at line 375 of file AbstractModel.php.

376  {
377  return $this->getConditions()->validate($object);
378  }

◆ validateData()

validateData ( \Magento\Framework\DataObject  $dataObject)

Validate rule data

Parameters
\Magento\Framework\DataObject$dataObject
Returns
bool|string[] - return true if validation passed successfully. Array with errors description otherwise @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 388 of file AbstractModel.php.

389  {
390  $result = [];
391  $fromDate = $toDate = null;
392 
393  if ($dataObject->hasFromDate() && $dataObject->hasToDate()) {
394  $fromDate = $dataObject->getFromDate();
395  $toDate = $dataObject->getToDate();
396  }
397 
398  if ($fromDate && $toDate) {
399  $fromDate = new \DateTime($fromDate);
400  $toDate = new \DateTime($toDate);
401 
402  if ($fromDate > $toDate) {
403  $result[] = __('End Date must follow Start Date.');
404  }
405  }
406 
407  if ($dataObject->hasWebsiteIds()) {
408  $websiteIds = $dataObject->getWebsiteIds();
409  if (empty($websiteIds)) {
410  $result[] = __('Please specify a website.');
411  }
412  }
413  if ($dataObject->hasCustomerGroupIds()) {
414  $customerGroupIds = $dataObject->getCustomerGroupIds();
415  if (empty($customerGroupIds)) {
416  $result[] = __('Please specify Customer Groups.');
417  }
418  }
419 
420  return !empty($result) ? $result : true;
421  }
__()
Definition: __.php:13

Field Documentation

◆ $_actions

$_actions
protected

Definition at line 32 of file AbstractModel.php.

◆ $_conditions

$_conditions
protected

Definition at line 25 of file AbstractModel.php.

◆ $_form

$_form
protected

Definition at line 39 of file AbstractModel.php.

◆ $_formFactory

$_formFactory
protected

Definition at line 80 of file AbstractModel.php.

◆ $_isDeleteable

$_isDeleteable = true
protected

Definition at line 46 of file AbstractModel.php.

◆ $_isReadonly

$_isReadonly = false
protected

Definition at line 53 of file AbstractModel.php.

◆ $_localeDate

$_localeDate
protected

Definition at line 87 of file AbstractModel.php.

◆ $serializer

$serializer
protected

Definition at line 59 of file AbstractModel.php.


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