Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Meta.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\SalesSequence\Model\MetaFactory;
14 use Magento\SalesSequence\Model\Profile as ModelProfile;
15 
23 {
29  protected $_eventPrefix = 'sales_sequence_meta';
30 
34  protected $resourceProfile;
35 
39  protected $metaFactory;
40 
47  public function __construct(
48  DatabaseContext $context,
49  MetaFactory $metaFactory,
50  ResourceProfile $resourceProfile,
51  $connectionName = null
52  ) {
53  $this->metaFactory = $metaFactory;
54  $this->resourceProfile = $resourceProfile;
55  parent::__construct($context, $connectionName);
56  }
57 
63  protected function _construct()
64  {
65  $this->_init('sales_sequence_meta', 'meta_id');
66  }
67 
77  {
78  $meta = $this->metaFactory->create();
79  $connection = $this->getConnection();
80  $bind = ['entity_type' => $entityType, 'store_id' => $storeId];
81  $select = $connection->select()->from(
82  $this->getMainTable(),
83  [$this->getIdFieldName()]
84  )->where(
85  'entity_type = :entity_type AND store_id = :store_id'
86  );
87  $metaId = $connection->fetchOne($select, $bind);
88 
89  if ($metaId) {
90  $this->load($meta, $metaId);
91  }
92  return $meta;
93  }
94 
101  protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
102  {
103  $object->setData(
104  'active_profile',
105  $this->resourceProfile->loadActiveProfile($object->getId())
106  );
107  return $this;
108  }
109 
118  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
119  {
120  if (!$object->getData('active_profile') instanceof ModelProfile) {
121  throw new NoSuchEntityException(
122  __(
123  "The entity sequence profile wasn't added to the meta active profile. "
124  . "Verify the profile and try again."
125  )
126  );
127  }
128 
129  if (!$object->getData('entity_type')
130  || $object->getData('store_id') === null
131  || !$object->getData('sequence_table')
132  ) {
133  throw new Exception(__('Not enough arguments'));
134  }
135 
136  return $this;
137  }
138 
142  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
143  {
144  $profile = $object->getData('active_profile')
145  ->setMetaId($object->getId());
146  $this->resourceProfile->save($profile);
147  return $this;
148  }
149 }
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Meta.php:118
__()
Definition: __.php:13
loadByEntityTypeAndStore($entityType, $storeId)
Definition: Meta.php:76
load(\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
Definition: AbstractDb.php:339
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: Meta.php:101
$connection
Definition: bulk.php:13
__construct(DatabaseContext $context, MetaFactory $metaFactory, ResourceProfile $resourceProfile, $connectionName=null)
Definition: Meta.php:47
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Meta.php:142