176 \
Magento\Framework\Data\Collection\AbstractDb $resourceCollection =
null,
180 $this->_appState = $context->getAppState();
181 $this->_eventManager = $context->getEventDispatcher();
182 $this->_cacheManager = $context->getCacheManager();
184 $this->_resourceCollection = $resourceCollection;
185 $this->_logger = $context->getLogger();
186 $this->_actionValidator = $context->getActionValidator();
188 if (method_exists($this->_resource,
'getIdFieldName')
191 $this->_idFieldName = $this->
_getResource()->getIdFieldName();
194 parent::__construct(
$data);
216 $this->_idFieldName = $this->
_getResource()->getIdFieldName();
234 '_resourceCollection',
253 $this->_appState = $context->getAppState();
254 $this->_eventManager = $context->getEventDispatcher();
255 $this->_cacheManager = $context->getCacheManager();
256 $this->_logger = $context->getLogger();
257 $this->_actionValidator = $context->getActionValidator();
269 $this->_idFieldName =
$name;
290 return $this->
_getData($this->_idFieldName);
314 if ($isDeleted !==
null) {
315 $this->_isDeleted = $isDeleted;
347 if ($key === (array)$key) {
348 if ($this->_data !== $key) {
349 $this->_hasDataChanges =
true;
353 if (!array_key_exists($key, $this->_data) || $this->_data[$key] !==
$value) {
354 $this->_hasDataChanges =
true;
356 $this->_data[$key] =
$value;
371 }
elseif (is_string($key)) {
372 if (isset($this->_data[$key]) || array_key_exists($key, $this->_data)) {
373 $this->_hasDataChanges =
true;
374 unset($this->_data[$key]);
376 }
elseif ($key === (array)$key) {
392 $this->_hasDataChanges = (bool)
$value;
407 if (isset($this->_origData[$key])) {
408 return $this->_origData[$key];
427 $this->_origData[$key] =
$data;
440 $newData = $this->
getData($field);
442 return $newData != $origData;
456 $this->_resourceName = $resourceName;
457 if ($collectionName ===
null) {
458 $collectionName = $resourceName .
'\\' .
'Collection';
460 $this->_collectionName = $collectionName;
472 if (empty($this->_resourceName) && empty($this->_resource)) {
473 throw new \Magento\Framework\Exception\LocalizedException(
488 return $this->_resource ? get_class($this->_resource) : ($this->_resourceName ? $this->_resourceName :
null);
501 if (empty($this->_resourceCollection) && empty($this->_collectionName)) {
502 throw new \Magento\Framework\Exception\LocalizedException(
503 new \
Magento\Framework\
Phrase(
'Model collection resource name is not defined.')
506 return $this->_resourceCollection ? clone $this
509 $this->_collectionName
535 public function load($modelId, $field =
null)
549 'data_object' => $this,
550 $this->_eventObject => $this,
563 $params = [
'object' => $this,
'field' => $field,
'value' => $modelId];
564 $this->_eventManager->dispatch(
'model_load_before',
$params);
566 $this->_eventManager->dispatch($this->_eventPrefix .
'_load_before',
$params);
577 $this->_eventManager->dispatch(
'model_load_after', [
'object' => $this]);
578 $this->_eventManager->dispatch($this->_eventPrefix .
'_load_after', $this->
_getEventData());
603 $this->updateStoredData();
633 $this->_hasDataChanges = $flag;
659 $this->_eventManager->dispatch(
'model_save_commit_after', [
'object' => $this]);
660 $this->_eventManager->dispatch($this->_eventPrefix .
'_save_commit_after', $this->
_getEventData());
675 if ($flag !==
null) {
676 $this->_isObjectNew = $flag;
678 if ($this->_isObjectNew !==
null) {
681 return !(bool)$this->
getId();
691 if (!$this->
getId()) {
694 $this->_eventManager->dispatch(
'model_save_before', [
'object' => $this]);
695 $this->_eventManager->dispatch($this->_eventPrefix .
'_save_before', $this->
_getEventData());
708 if ($validator && !$validator->isValid($this)) {
709 $errors = $validator->getMessages();
710 $exception = new \Magento\Framework\Validator\Exception(
713 foreach (
$errors as $errorMessage) {
714 $exception->addMessage(
new \
Magento\Framework\Message\Error($errorMessage));
729 if ($this->_validatorBeforeSave ===
null) {
744 $resourceRules = $this->
_getResource()->getValidationRulesBeforeSave();
745 if (!$modelRules && !$resourceRules) {
749 if ($modelRules && $resourceRules) {
750 $validator = new \Zend_Validate();
751 $validator->addValidator($modelRules);
752 $validator->addValidator($resourceRules);
754 $validator = $modelRules;
756 $validator = $resourceRules;
781 if ($this->_cacheTag) {
782 if ($this->_cacheTag ===
true) {
785 if (is_array($this->_cacheTag)) {
803 if ($tags !==
false) {
804 $this->_cacheManager->clean($tags);
817 $this->_eventManager->dispatch(
'model_save_after', [
'object' => $this]);
818 $this->_eventManager->dispatch(
'clean_cache_by_tags', [
'object' => $this]);
819 $this->_eventManager->dispatch($this->_eventPrefix .
'_save_after', $this->
_getEventData());
820 $this->updateStoredData();
833 public function delete()
847 if (!$this->_actionValidator->isAllowed($this)) {
848 throw new \Magento\Framework\Exception\LocalizedException(
849 new \
Magento\Framework\
Phrase(
'Delete operation is forbidden for current area')
853 $this->_eventManager->dispatch(
'model_delete_before', [
'object' => $this]);
854 $this->_eventManager->dispatch($this->_eventPrefix .
'_delete_before', $this->
_getEventData());
866 $this->_eventManager->dispatch(
'model_delete_after', [
'object' => $this]);
867 $this->_eventManager->dispatch(
'clean_cache_by_tags', [
'object' => $this]);
868 $this->_eventManager->dispatch($this->_eventPrefix .
'_delete_after', $this->
_getEventData());
869 $this->storedData = [];
880 $this->_eventManager->dispatch(
'model_delete_commit_after', [
'object' => $this]);
881 $this->_eventManager->dispatch($this->_eventPrefix .
'_delete_commit_after', $this->
_getEventData());
903 return $this->
_getData(
'entity_id');
914 return $this->
setData(
'entity_id', $entityId);
925 $this->_eventManager->dispatch($this->_eventPrefix .
'_clear', $this->
_getEventData());
955 private function updateStoredData()
957 if (isset($this->_data)) {
960 $this->storedData = [];
setData($key, $value=null)
getData($key='', $index=null)
elseif(isset( $params[ 'redirect_parent']))
_createValidatorBeforeSave()
load($modelId, $field=null)
__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=[])
setOrigData($key=null, $data=null)
_getValidatorBeforeSave()
_setResourceModel($resourceName, $collectionName=null)
beforeLoad($identifier, $field=null)
_beforeLoad($modelId, $field=null)
_getValidationRulesBeforeSave()
dataHasChangedFor($field)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
isDeleted($isDeleted=null)
if(!isset($_GET['name'])) $name