function execute($params) { // パラメータを調整 $post = Vizualizer::request(); $month = $post["ym"]; if (empty($month) || preg_match("/[0-9]{4}-[0-9]{2}/", $month) == 0) { $month = date("Y-m"); } // クエリを生成 $loader = new Vizualizer_Plugin("stock"); $orders = $loader->loadTable("Orders"); $select = new Vizualizer_Query_Select($orders); $select->addColumn("SUBSTR(" . $orders->order_date . ", 1, 10)", "order_date"); $select->addColumn("SUM(" . $orders->price . ")", "price"); $select->where("order_date LIKE ?", array($month . "-%")); $select->group("SUBSTR(" . $orders->order_date . ", 1, 10)"); // 生成したクエリに対して検索を実行し、結果をモデルとして取得 $order = $loader->loadModel("Order"); $orders = $order->queryAllBy($select); // 結果を属性に設定 $attr = Vizualizer::attr(); $attr["sales"] = $orders; $attr["thismonth"] = date("Y-m-01", strtotime($month . "-01")); $attr["nextmonth"] = date("Y-m", strtotime("+1 month", strtotime($month . "-01"))); $attr["lastmonth"] = date("Y-m", strtotime("-1 month", strtotime($month . "-01"))); }
/** * セッションのデータををDBから読み込む. * * @param string $id セッションID * @return string セッションデータの値 */ function read($id) { $id_key = $this->id_key; $data_key = $this->data_key; // セッションデータを取得する。 $select = new Vizualizer_Query_Select($this->table); $select->addColumn($this->table->_W); $select->addWhere($this->table->{$id_key} . " = ?", array($id)); $result = $select->execute(); return $result[0][$data_key]; }
/** * レコードの件数を取得する。 */ public function countBy($values = array(), $columns = "*") { $select = new Vizualizer_Query_Select($this->access, $this->viewTable); $select->addColumn("COUNT(" . $columns . ") AS count"); if (is_array($values)) { foreach ($values as $key => $value) { $select = $this->appendWhere($select, $key, $value); } } $result = $select->execute(); if (count($result) > 0) { return $result[0]["count"]; } else { return "0"; } }
/** * 指定したトランザクション内にて主キーベースでデータの保存を行う。 * 主キーが存在しない場合は何もしない。 * また、モデル内のカラムがDBに無い場合はスキップする。 * データ作成日/更新日は自動的に設定される。 */ public function save() { if (!empty($this->primary_keys)) { // 現在該当のデータが登録されているか調べる。 $pkset = false; $select = new Vizualizer_Query_Select($this->access); $select->addColumn($this->access->_W); foreach ($this->primary_keys as $key) { if (isset($this->values[$key])) { $select->addWhere($this->access->{$key} . " = ?", array($this->values[$key])); } else { $pkset = false; break; } $pkset = true; } if ($pkset) { $result = $select->execute(); } else { $result = array(); } if (!is_array($result) || empty($result)) { // 主キーのデータが無かった場合はデータを作成する。 $this->create(); } else { // 主キーのデータがあった場合はデータを更新する。 $this->update(); } } }