/** * Batch update some data. * * @param string $table Table name. * @param string $data Data you want to update. * @param mixed $conditions Where conditions, you can use array or Compare object. * Example: * - `array('id' => 5)` => id = 5 * - `new GteCompare('id', 20)` => 'id >= 20' * - `new Compare('id', '%Flower%', 'LIKE')` => 'id LIKE "%Flower%"' * * @return boolean True if update success. */ public function updateBatch($table, $data, $conditions = array()) { $query = $this->db->getQuery(true); // Build conditions $query = QueryHelper::buildWheres($query, $conditions); // Build update values. $fields = array_keys($this->getColumns($table)); $hasField = false; foreach ((array) $data as $field => $value) { if (!in_array($field, $fields)) { continue; } $query->set($query->format('%n = %q', $field, $value)); $hasField = true; } if (!$hasField) { return false; } $query->update($table); return $this->db->setQuery($query)->execute(); }
/** * Do find action. * * @param array $conditions Where conditions, you can use array or Compare object. * @param array $orders Order sort, can ba string, array or object. * @param integer $start Limit start number. * @param integer $limit Limit rows. * * @return mixed Found rows data set. */ protected function doFind(array $conditions, array $orders, $start, $limit) { $query = $this->db->getQuery(true); // Conditions. $query = QueryHelper::buildWheres($query, $conditions); // Loop ordering foreach ($orders as $order) { $query->order($order); } // Build query $selectType = $this->selectType ?: QueryHelper::COLS_WITH_FIRST; $query->select($this->select ?: $this->queryHelper->getSelectFields($selectType)); $this->queryHelper->registerQueryTables($query); return $this->db->setQuery($query, $start, $limit)->loadObjectList(); }
/** * Do delete action, this method should be override by sub class. * * @param mixed $conditions Where conditions, you can use array or Compare object. * * @throws \Exception * @return boolean Will be always true. */ protected function doDelete(array $conditions) { $query = $this->db->getQuery(true); // Conditions. QueryHelper::buildWheres($query, $conditions); $query->delete($this->table); $this->db->transactionStart(true); try { $result = (bool) $this->db->setQuery($query)->execute(); } catch (\Exception $e) { $this->db->transactionRollback(true); throw $e; } $this->db->transactionCommit(true); return $result; }