/** * Returns an array of items for a page. * * @param integer $offset Page offset * @param integer $itemCountPerPage Number of items per page * @return array */ public function getItems($offset, $itemCountPerPage) { // Cast to integers, as $itemCountPerPage can be string sometimes and that would fail later checks $offset = (int) $offset; $itemCountPerPage = (int) $itemCountPerPage; if ($this->_lastOffset === $offset && $this->_lastItemCount === $itemCountPerPage && null !== $this->_lastItems) { return $this->_lastItems; } $this->_lastOffset = $offset; $this->_lastItemCount = $itemCountPerPage; // Optimization: by using the MySQL feature SQL_CALC_FOUND_ROWS // we can get the count and the results in a single query. $db = $this->_select->getAdapter(); if (null === $this->_count && $db instanceof \Zend_Db_Adapter_Mysqli) { $this->_select->limit($itemCountPerPage, $offset); $sql = $this->_select->__toString(); if (\MUtil_String::startsWith($sql, 'select ', true)) { $sql = 'SELECT SQL_CALC_FOUND_ROWS ' . substr($sql, 7); } $this->_lastItems = $db->fetchAll($sql); $this->_count = $db->fetchOne('SELECT FOUND_ROWS()'); } else { $this->_lastItems = $this->_selectAdapter->getItems($offset, $itemCountPerPage); } if (is_array($this->_lastItems)) { if (isset($this->_model->prefetchIterator) && $this->_model->prefetchIterator) { $this->_lastItems = new \ArrayIterator($this->_lastItems); } $this->_lastItems = $this->_model->processAfterLoad($this->_lastItems, false, false); } return $this->_lastItems; }
/** * Create a model that joins two or more tables * * @param string $name A name for the model * @param string $startTable The base table for the model * @param mixed $saveable Will changes to this table be saved, true or a combination of SAVE_MODE constants */ public function __construct($name, $startTable, $saveable = false) { parent::__construct($name); $alias = $this->_loadTable($startTable, $saveable); $table = $this->_tables[$alias]; // Fix primary keys to those of the current table. $this->getKeys(); $this->_select = new \Zend_Db_Select($table->getAdapter()); $this->_select->from(array($alias => $this->_getTableName($table)), array()); }
/** * * @param \Zend_Db_Table_Abstract $table An Zend abstract table or the table name * @param string $altName An alternative name to use, default is the name of the table itself */ public function __construct($table, $altName = null) { if ($table instanceof \Zend_Db_Table_Abstract) { $this->_table = $table; $table_name = $this->_getTableName($table); } else { $this->_table = new \Zend_Db_Table($table); $table_name = $table; } parent::__construct(null === $altName ? $table_name : $altName); $this->_loadTableMetaData($this->_table); }
/** * * @param \Zend_Db_Select $select * @param string $name Optiona name */ public function __construct(\Zend_Db_Select $select, $name = null) { $this->_select = $select; // Make sure the columns are known to the model foreach ($select->getPart(\Zend_Db_Select::COLUMNS) as $column) { if (isset($column[2])) { $this->set($column[2]); } elseif (is_string($column[1])) { $this->set($column[1]); } } if (null === $name) { $name = 'rnd' . rand(10000, 999999); } parent::__construct($name); }
/** * * @param string $filter The text to filter for * @param string $name The model field name * @param string $sqlField The SQL field name * @param \MUtil_Model_DatabaseModelAbstract $model * @return array Array of OR-filter statements */ public function textFilter($filter, $name, $sqlField, \MUtil_Model_DatabaseModelAbstract $model) { $options = $model->get($name, 'multiOptions'); if ($options) { $adapter = $model->getAdapter(); $wheres = array(); foreach ($options as $key => $value) { // \MUtil_Echo::track($key, $value, $filter, stripos($value, $filter)); if (stripos($value, $filter) === false) { continue; } if (null === $key) { $wheres[] = $sqlField . ' IS NULL'; } else { $wheres[] = $adapter->quoteInto($sqlField . " LIKE ?", '%' . $this->seperatorChar . $key . $this->seperatorChar . '%'); if (!$this->valuePad) { // Add other options $wheres[] = $adapter->quoteInto($sqlField . " LIKE ?", $key . $this->seperatorChar . '%'); $wheres[] = $adapter->quoteInto($sqlField . " LIKE ?", '%' . $this->seperatorChar . $key); $wheres[] = $adapter->quoteInto($sqlField . " = ?", $key); } } } return $wheres; } }
/** * Function that automatically fills changed, changed_by, created and created_by fields with a certain prefix. * * @param \MUtil_Model_DatabaseModelAbstract $model * @param string $prefix Three letter code * @param int $userid Gems user id */ public static function setChangeFieldsByPrefix(\MUtil_Model_DatabaseModelAbstract $model, $prefix, $userid = null) { $changed_field = $prefix . '_changed'; $changed_by_field = $prefix . '_changed_by'; $created_field = $prefix . '_created'; $created_by_field = $prefix . '_created_by'; foreach (array($changed_field, $changed_by_field, $created_field, $created_by_field) as $field) { $model->set($field, 'elementClass', 'none'); } $model->setOnSave($changed_field, new \MUtil_Db_Expr_CurrentTimestamp()); $model->setSaveOnChange($changed_field); $model->setOnSave($created_field, new \MUtil_Db_Expr_CurrentTimestamp()); $model->setSaveWhenNew($created_field); if (!$userid) { $userid = \GemsEscort::getInstance()->currentUser->getUserId(); if (!$userid) { $userid = 1; } } if ($userid) { $model->setOnSave($changed_by_field, $userid); $model->setSaveOnChange($changed_by_field); $model->setOnSave($created_by_field, $userid); $model->setSaveWhenNew($created_by_field); } }