📄 abstract.php
字号:
/** * If this table uses a database sequence object and the data does not * specify a value, then get the next ID from the sequence and add it * to the row. We assume that only the first column in a compound * primary key takes a value from a sequence. */ if (is_string($this->_sequence) && !isset($data[$pkIdentity])) { $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence); } /** * INSERT the new row. */ $this->_db->insert($this->_name, $data); if (isset($data[$pkIdentity])) { /** * Return the primary key value or array of values(s) if the * primary key is compound. This handles the case of natural keys * and sequence-driven keys. This also covers the case of * auto-increment keys when the user specifies a value, thus * overriding the auto-increment logic. */ $pkData = array_intersect_key($data, array_flip($primary)); if (count($primary) == 1) { return current($pkData); } else { return $pkData; } } if ($this->_sequence === true) { /** * Return the most recent ID generated by an auto-increment * or IDENTITY column. */ return $this->_db->lastInsertId(); } /** * The last case: the user did not specify a value for the primary * key, nor is this table class declared to use an auto-increment key. * Since the insert did not fail, we can assume this is one of the edge * cases, which may include: * - the table has no primary key defined; * - the database table uses a trigger to set a primary key value; * - the RDBMS permits primary keys to be NULL or have a value set * to the column's DEFAULT */ return null; } /** * Updates existing rows. * * @param array $data Column-value pairs. * @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses. * @return int The number of rows updated. */ public function update(array $data, $where) { return $this->_db->update($this->_name, $data, $where); } /** * Called by a row object for the parent table's class during save() method. * * @param string $parentTableClassname * @param array $oldPrimaryKey * @param array $newPrimaryKey * @return int */ public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey) { $rowsAffected = 0; foreach ($this->_getReferenceMapNormalized() as $rule => $map) { if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) { switch ($map[self::ON_UPDATE]) { case self::CASCADE: $newRefs = array(); for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) { $col = $this->_db->foldCase($map[self::COLUMNS][$i]); $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]); if (array_key_exists($refCol, $newPrimaryKey)) { $newRefs[$col] = $newPrimaryKey[$refCol]; } $where[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($col, true) . ' = ?', $oldPrimaryKey[$refCol] ); } $rowsAffected += $this->update($newRefs, $where); break; default: // no action break; } } } return $rowsAffected; } /** * Deletes existing rows. * * @param array|string $where SQL WHERE clause(s). * @return int The number of rows deleted. */ public function delete($where) { return $this->_db->delete($this->_name, $where); } /** * Called by parent table's class during delete() method. * * @param string $parentTableClassname * @param array $primaryKey * @return int Number of affected rows */ public function _cascadeDelete($parentTableClassname, array $primaryKey) { $rowsAffected = 0; foreach ($this->_getReferenceMapNormalized() as $rule => $map) { if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) { switch ($map[self::ON_DELETE]) { case self::CASCADE: for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) { $col = $this->_db->foldCase($map[self::COLUMNS][$i]); $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]); $where[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($col, true) . ' = ?', $primaryKey[$refCol] ); } $rowsAffected += $this->delete($where); break; default: // no action break; } } } return $rowsAffected; } /** * Fetches rows by primary key. * The arguments specify the primary key values. * If the table has a multi-column primary key, you must * pass as many arguments as the count of column in the * primary key. * * To find multiple rows by primary key, the argument * should be an array. If the table has a multi-column * primary key, all arguments must be arrays with the * same number of elements. * * The find() method always returns a Rowset object, * even if only one row was found. * * @param mixed The value(s) of the primary key. * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria. * @throws Zend_Db_Table_Exception */ public function find() { $args = func_get_args(); $keyNames = array_values((array) $this->_primary); if (empty($args)) { require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("No value(s) specified for the primary key"); } if (count($args) != count($keyNames)) { require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Missing value(s) for the primary key"); } $whereList = array(); $numberTerms = 0; foreach ($args as $keyPosition => $keyValues) { // Coerce the values to an array. // Don't simply typecast to array, because the values // might be Zend_Db_Expr objects. if (!is_array($keyValues)) { $keyValues = array($keyValues); } if ($numberTerms == 0) { $numberTerms = count($keyValues); } else if (count($keyValues) != $numberTerms) { require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Missing value(s) for the primary key"); } for ($i = 0; $i < count($keyValues); ++$i) { $whereList[$i][$keyPosition] = $keyValues[$i]; } } $whereClause = null; if (count($whereList)) { $whereOrTerms = array(); foreach ($whereList as $keyValueSets) { $whereAndTerms = array(); foreach ($keyValueSets as $keyPosition => $keyValue) { $whereAndTerms[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($keyNames[$keyPosition], true) . ' = ?', $keyValue ); } $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')'; } $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')'; } return $this->fetchAll($whereClause); } /** * Fetches all rows. * * Honors the Zend_Db_Adapter fetch mode. * * @param string|array $where OPTIONAL An SQL WHERE clause. * @param string|array $order OPTIONAL An SQL ORDER clause. * @param int $count OPTIONAL An SQL LIMIT count. * @param int $offset OPTIONAL An SQL LIMIT offset. * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode. */ public function fetchAll($where = null, $order = null, $count = null, $offset = null) { $data = array( 'table' => $this, 'data' => $this->_fetch($where, $order, $count, $offset), 'rowClass' => $this->_rowClass, 'stored' => true ); Zend_Loader::loadClass($this->_rowsetClass); return new $this->_rowsetClass($data); } /** * Fetches one row in an object of type Zend_Db_Table_Row_Abstract, * or returns Boolean false if no row matches the specified criteria. * * @param string|array $where OPTIONAL An SQL WHERE clause. * @param string|array $order OPTIONAL An SQL ORDER clause. * @return Zend_Db_Table_Row_Abstract The row results per the * Zend_Db_Adapter fetch mode, or null if no row found. */ public function fetchRow($where = null, $order = null) { $keys = array_values((array) $this->_primary); $vals = array_fill(0, count($keys), null); $primary = array_combine($keys, $vals); $rows = $this->_fetch($where, $order, 1); if (count($rows) == 0) { return null; } $data = array( 'table' => $this, 'data' => $rows[0], 'stored' => true ); Zend_Loader::loadClass($this->_rowClass); return new $this->_rowClass($data); } /** * Fetches a new blank row (not from the database). * * @return Zend_Db_Table_Row_Abstract * @deprecated since 0.9.3 - use createRow() instead. */ public function fetchNew() { return $this->createRow(); } /** * Fetches a new blank row (not from the database). * * @param array $data OPTIONAL data to populate in the new row. * @return Zend_Db_Table_Row_Abstract */ public function createRow(array $data = array()) { $defaults = array_combine($this->_cols, array_fill(0, count($this->_cols), null)); $keys = array_flip($this->_cols); $data = array_intersect_key($data, $keys); $data = array_merge($defaults, $data); /** * If the primary key can be generated automatically, and no value was * specified in the user-supplied data, then omit it from the tuple. */ $primary = (array) $this->_primary; $pkIdentity = $primary[(int)$this->_identity]; if ($data[$pkIdentity] === null) { unset($data[$pkIdentity]); } $config = array( 'table' => $this, 'data' => $data, 'stored' => false ); Zend_Loader::loadClass($this->_rowClass); return new $this->_rowClass($config); } /** * Support method for fetching rows. * * @param string|array $where OPTIONAL An SQL WHERE clause. * @param string|array $order OPTIONAL An SQL ORDER clause. * @param int $count OPTIONAL An SQL LIMIT count. * @param int $offset OPTIONAL An SQL LIMIT offset. * @return array The row results, in FETCH_ASSOC mode. */ protected function _fetch($where = null, $order = null, $count = null, $offset = null) { // selection tool $select = $this->_db->select(); // the FROM clause $select->from($this->_name, $this->_cols, $this->_schema); // the WHERE clause $where = (array) $where; foreach ($where as $key => $val) { // is $key an int? if (is_int($key)) { // $val is the full condition $select->where($val); } else { // $key is the condition with placeholder, // and $val is quoted into the condition $select->where($key, $val); } } // the ORDER clause if (!is_array($order)) { $order = array($order); } foreach ($order as $val) { $select->order($val); } // the LIMIT clause $select->limit($count, $offset); // return the results $stmt = $this->_db->query($select); $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); return $data; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -