Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Variable.php
Go to the documentation of this file.
1 <?php
8 
15 {
21  protected function _construct()
22  {
23  $this->_init('variable', 'variable_id');
24  }
25 
33  public function loadByCode(\Magento\Variable\Model\Variable $object, $code)
34  {
35  if ($result = $this->getVariableByCode($code, true, $object->getStoreId())) {
36  $object->setData($result);
37  }
38  return $this;
39  }
40 
49  public function getVariableByCode($code, $withValue = false, $storeId = 0)
50  {
51  $select = $this->getConnection()->select()->from(
52  $this->getMainTable()
53  )->where(
54  $this->getMainTable() . '.code = ?',
55  $code
56  );
57  if ($withValue) {
59  }
60  return $this->getConnection()->fetchRow($select);
61  }
62 
69  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
70  {
71  parent::_afterSave($object);
72  if ($object->getUseDefaultValue()) {
73  /*
74  * remove store value
75  */
76  $this->getConnection()->delete(
77  $this->getTable('variable_value'),
78  ['variable_id = ?' => $object->getId(), 'store_id = ?' => $object->getStoreId()]
79  );
80  } else {
81  $data = [
82  'variable_id' => $object->getId(),
83  'store_id' => $object->getStoreId(),
84  'plain_value' => $object->getPlainValue(),
85  'html_value' => $object->getHtmlValue(),
86  ];
87  $data = $this->_prepareDataForTable(
88  new \Magento\Framework\DataObject($data),
89  $this->getTable('variable_value')
90  );
91  $this->getConnection()->insertOnDuplicate(
92  $this->getTable('variable_value'),
93  $data,
94  ['plain_value', 'html_value']
95  );
96  }
97  return $this;
98  }
99 
108  protected function _getLoadSelect($field, $value, $object)
109  {
110  $select = parent::_getLoadSelect($field, $value, $object);
111  $this->_addValueToSelect($select, $object->getStoreId());
112  return $select;
113  }
114 
122  protected function _addValueToSelect(
123  \Magento\Framework\DB\Select $select,
124  $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID
125  ) {
126  $connection = $this->getConnection();
127  $ifNullPlainValue = $connection->getCheckSql(
128  'store.plain_value IS NULL',
129  'def.plain_value',
130  'store.plain_value'
131  );
132  $ifNullHtmlValue = $connection->getCheckSql('store.html_value IS NULL', 'def.html_value', 'store.html_value');
133 
134  $select->joinLeft(
135  ['def' => $this->getTable('variable_value')],
136  'def.variable_id = ' . $this->getMainTable() . '.variable_id AND def.store_id = 0',
137  []
138  )->joinLeft(
139  ['store' => $this->getTable('variable_value')],
140  'store.variable_id = def.variable_id AND store.store_id = ' . $connection->quote($storeId),
141  []
142  )->columns(
143  [
144  'plain_value' => $ifNullPlainValue,
145  'html_value' => $ifNullHtmlValue,
146  'store_plain_value' => 'store.plain_value',
147  'store_html_value' => 'store.html_value',
148  ]
149  );
150 
151  return $this;
152  }
153 }
_getLoadSelect($field, $value, $object)
Definition: Variable.php:108
loadByCode(\Magento\Variable\Model\Variable $object, $code)
Definition: Variable.php:33
$value
Definition: gender.phtml:16
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Variable.php:69
_addValueToSelect(\Magento\Framework\DB\Select $select, $storeId=\Magento\Store\Model\Store::DEFAULT_STORE_ID)
Definition: Variable.php:122
$connection
Definition: bulk.php:13
getVariableByCode($code, $withValue=false, $storeId=0)
Definition: Variable.php:49
$code
Definition: info.phtml:12