Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
37 {
41  const ERROR_NO_RECORD_FOUND = 'noRecordFound';
42  const ERROR_RECORD_FOUND = 'recordFound';
43 
47  protected $_messageTemplates = array(
48  self::ERROR_NO_RECORD_FOUND => "No record matching '%value%' was found",
49  self::ERROR_RECORD_FOUND => "A record matching '%value%' was found",
50  );
51 
55  protected $_schema = null;
56 
60  protected $_table = '';
61 
65  protected $_field = '';
66 
70  protected $_exclude = null;
71 
77  protected $_adapter = null;
78 
83  protected $_select;
84 
102  public function __construct($options)
103  {
104  if ($options instanceof Zend_Db_Select) {
105  $this->setSelect($options);
106  return;
107  }
108  if ($options instanceof Zend_Config) {
109  $options = $options->toArray();
110  } else if (func_num_args() > 1) {
111  $options = func_get_args();
112  $temp['table'] = array_shift($options);
113  $temp['field'] = array_shift($options);
114  if (!empty($options)) {
115  $temp['exclude'] = array_shift($options);
116  }
117 
118  if (!empty($options)) {
119  $temp['adapter'] = array_shift($options);
120  }
121 
122  $options = $temp;
123  }
124 
125  if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
126  #require_once 'Zend/Validate/Exception.php';
127  throw new Zend_Validate_Exception('Table or Schema option missing!');
128  }
129 
130  if (!array_key_exists('field', $options)) {
131  #require_once 'Zend/Validate/Exception.php';
132  throw new Zend_Validate_Exception('Field option missing!');
133  }
134 
135  if (array_key_exists('adapter', $options)) {
136  $this->setAdapter($options['adapter']);
137  }
138 
139  if (array_key_exists('exclude', $options)) {
140  $this->setExclude($options['exclude']);
141  }
142 
143  $this->setField($options['field']);
144  if (array_key_exists('table', $options)) {
145  $this->setTable($options['table']);
146  }
147 
148  if (array_key_exists('schema', $options)) {
149  $this->setSchema($options['schema']);
150  }
151  }
152 
159  public function getAdapter()
160  {
164  if ($this->_adapter === null) {
165  $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
166  if (null === $this->_adapter) {
167  #require_once 'Zend/Validate/Exception.php';
168  throw new Zend_Validate_Exception('No database adapter present');
169  }
170  }
171  return $this->_adapter;
172  }
173 
181  public function setAdapter($adapter)
182  {
183  if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
184  #require_once 'Zend/Validate/Exception.php';
185  throw new Zend_Validate_Exception('Adapter option must be a database adapter!');
186  }
187 
188  $this->_adapter = $adapter;
189  return $this;
190  }
191 
197  public function getExclude()
198  {
199  return $this->_exclude;
200  }
201 
208  public function setExclude($exclude)
209  {
210  $this->_exclude = $exclude;
211  return $this;
212  }
213 
219  public function getField()
220  {
221  return $this->_field;
222  }
223 
230  public function setField($field)
231  {
232  $this->_field = (string) $field;
233  return $this;
234  }
235 
241  public function getTable()
242  {
243  return $this->_table;
244  }
245 
252  public function setTable($table)
253  {
254  $this->_table = (string) $table;
255  return $this;
256  }
257 
263  public function getSchema()
264  {
265  return $this->_schema;
266  }
267 
274  public function setSchema($schema)
275  {
276  $this->_schema = $schema;
277  return $this;
278  }
279 
287  public function setSelect($select)
288  {
289  if (!$select instanceof Zend_Db_Select) {
290  throw new Zend_Validate_Exception('Select option must be a valid ' .
291  'Zend_Db_Select object');
292  }
293  $this->_select = $select;
294  return $this;
295  }
296 
305  public function getSelect()
306  {
307  if (null === $this->_select) {
308  $db = $this->getAdapter();
312  $select = new Zend_Db_Select($db);
313  $select->from($this->_table, array($this->_field), $this->_schema);
314  if ($db->supportsParameters('named')) {
315  $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named
316  } else {
317  $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional
318  }
319  if ($this->_exclude !== null) {
320  if (is_array($this->_exclude)) {
321  $select->where(
322  $db->quoteIdentifier($this->_exclude['field'], true) .
323  ' != ?', $this->_exclude['value']
324  );
325  } else {
326  $select->where($this->_exclude);
327  }
328  }
329  $select->limit(1);
330  $this->_select = $select;
331  }
332  return $this->_select;
333  }
334 
341  protected function _query($value)
342  {
343  $select = $this->getSelect();
347  $result = $select->getAdapter()->fetchRow(
348  $select,
349  array('value' => $value), // this should work whether db supports positional or named params
351  );
352 
353  return $result;
354  }
355 }
const FETCH_ASSOC
Definition: Db.php:142
$adapter
Definition: webapi_user.php:16
$value
Definition: gender.phtml:16
static getDefaultAdapter()
Definition: Abstract.php:571
$table
Definition: trigger.php:14