/** * Allows one to quickly do a NOT IN MySQL query. * * @param string $field - The field to check against. * @param array $values */ public function whereFieldValueIsNotIn($field, $values) { if (!count($values)) { return $this; } // Escape the values. $inString = ''; foreach ($values as $value) { $inString .= $this->_zendDb->quote($value) . ','; } $inString = rtrim($inString, ','); $this->where($this->_zendDb->quoteIdentifier($this->tableNameOrAlias() . '.' . $field) . ' NOT IN (' . $inString . ')'); return $this; }
/** * Quote Into * * This method overrides Zend_Db::where() so that we can have a little * more flexibility. Specifically, there are 6 use cases for this method. * * $db->quoteInto('id = 5'); * * A SQL string is passed. If this is all that is passed, we forward along * to the Zend_DB_Select method. * * $db->quoteInto('firstname = ? OR lastname = ?', 'James'); * * A SQL string is passed along with a single replacement value for all "?". * In this case, we pass everything along as well since Zend_DB_Select handles this * use case. * * $db->quoteInto('firstname = ? AND lastname = ?', 'Tom', 'Jones'); * * A SQL string is passed along with a variable argument list, each representing * a "?" placeholder value in the string. * * $db->quoteInto('firstname = :0 AND lastname = :1', 'Tom', 'Jones'); * * A SQL string is passed along with a variable argument list, each representing * a ":index" placeholder value in the string. * * $db->quoteInto('firstname = :0 AND lastname = :1', array('Tom', 'Jones')); * * A SQL string is passed along with an array where each value represents * a ":index" placeholder value in the string. * * $db->quoteInto('firstname = :firstname AND lastname = :lastname', array('firstname' => 'Tom', 'lastname' => 'Jones')); * * A SQL string is passed along with an array where each value represents * a ":key" placeholder value in the string. * * @param string $sql A SQL string * @param string $args Variable list of arguments to replace into the string * @return string The SQL string with escaped quotes */ public function quoteInto($sql) { $args = func_get_args(); if (count($args) < 2 || count($args) == 2 && !is_array($args[1])) { // Nothing special happening here, pass it through to the default method. // We do this instead of calling $this->_zendDb->quoteInto($sql, $args[1]) // because we're not sure if args[1] is there or not. return call_user_func_array(array($this->_db, 'quoteInto'), $args); } // We are going to run our own replacement method. $replacements = $args; array_shift($replacements); if (is_array($replacements[0])) { // Here, we are passed an array of replacements with key/value combos // that correspond to ":key" => "sqlValueToBeEscaped". $replacements = $replacements[0]; } // If we are using "?" placeholders, we need to change them over to indexed-placeholders. $pieces = explode('?', $sql); $sql = ''; foreach ($pieces as $i => $piece) { $sql .= $piece; if ($i == count($pieces) - 1) { // We are on the last one, skip. break; } $sql .= ':' . $i; } // At this point, we are using ":key" placeholders. $pieces = preg_split('/:(\\w+)\\b/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $sql = ''; foreach ($pieces as $key => $piece) { if ($key % 2 == 0) { // We are on a piece of the query. $sql .= $piece; } else { // We are on a placeholder. $sql .= $this->_db->quote($replacements[$piece]); } } return $sql; }
/** * Saves the current attributes to the DB. */ public function save() { $this->beforeSave(); // Trims out non-fields and serializes data for DB entry. $info = $this->info(true, true); if (!$this->exists) { $this->beforeCreate(); $this->_zendDb->insert($this->tableName(), $info); if (!$this->id) { $this->id = $this->_zendDb->lastInsertId(); } $this->_afterCommand('insert'); $this->exists = true; $this->afterCreate(); } else { $this->_zendDb->update($this->tableName(), $info, 'id = ' . $this->_zendDb->quote($this->id)); $this->_afterCommand('update'); } $this->afterSave(); $this->_lastSaveRow = $this->info(); }