Source for file SC_DB_MasterData.php
Documentation is available at SC_DB_MasterData.php
* This file is part of EC-CUBE
* Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
* http://www.lockon.co.jp/
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* マスターデータは, DB に格納されているが, パフォーマンスを得るため,
* 初回のみ DBへアクセスし, データを定義したキャッシュファイルを生成する.
* マスターデータのテーブルは, 下記のようなカラムが必要がある.
* 上記カラムのデータ型は特に指定しないが, 1 と 2 は常に string 型となる.
* マスターデータがキャッシュされると, key => value 形式の配列として使用できる.
* マスターデータのキャッシュは, MASTER_DATA_REALDIR/マスターデータ名.php というファイルが生成される.
* @author LOCKON CO.,LTD.
* @version $Id:SC_DB_MasterData.php 15532 2007-08-31 14:39:46Z nanasess $
var $columns = array('id', 'name', 'rank', 'remarks');
* 1. MASTER_DATA_REALDIR にマスターデータキャッシュが存在しない場合、
* DBからマスターデータを取得して、マスターデータキャッシュを生成する。
* 2. マスターデータキャッシュを読み込み、変数に格納し返す。
* 返り値は, key => value 形式の配列である.
* @param string $name マスターデータ名
* @param array $columns [0] => キー, [1] => 表示文字列, [2] => 表示順
$filepath = MASTER_DATA_REALDIR . $name . '.serial';
* 引数 $masterData をマスターデータとしてDBに追加し,
* $masterData は key => value 形式の配列である必要がある.
* @param string $name マスターデータ名
* @param array $columns [0] => キー, [1] => 表示文字列, [2] => 表示順
* @param array $masterData マスターデータ
* @param bool $autoCommit トランザクションを自動的に commit する場合 true
* @return integer マスターデータの登録数
foreach ($masterData as $key => $val) {
$sqlVal = array($columns[0] => (string) $key,
$columns[1] => (string) $val,
$columns[2] => (string) $i);
$this->objQuery->insert($name, $sqlVal);
* 引数 $masterData の値でマスターデータを更新する.
* $masterData は key => value 形式の配列である必要がある.
* @param string $name マスターデータ名
* @param array $columns [0] => キー, [1] => 表示文字列, [2] => 表示順
* @param array $masterData マスターデータ
* @param bool $autoCommit トランザクションを自動的に commit する場合 true
* @return integer マスターデータの更新数
foreach ($masterData as $key => $val) {
$sqlVal = array($columns[1] => $val);
$this->objQuery->update($name, $sqlVal, $columns[0] . " = " . SC_Utils_Ex::sfQuoteSmart($key));
* 引数 $masterData の値でマスターデータを更新する.
* $masterData は key => value 形式の配列である必要がある.
* @param string $name マスターデータ名
* @param string $comment コメント
* @param bool $autoCommit トランザクションを自動的に commit する場合 true
* @return integer マスターデータの更新数
$sqlVal[$columns[0]] = $key;
$sqlVal[$columns[1]] = $value;
$sqlVal[$columns[2]] = $this->objQuery->max($columns[2], $name) + 1;
$sqlVal[$columns[3]] = $comment;
$this->objQuery->insert($name, $sqlVal);
* @param string $name マスターデータ名
* @param bool $autoCommit トランザクションを自動的に commit する場合 true
* @return integer マスターデータの削除数
$result = $this->objQuery->delete($name);
* @param string $name マスターデータ名
* @return bool 消去した場合 true
$masterDataFile = MASTER_DATA_REALDIR . $name . ".php";
$masterDataFile = MASTER_DATA_REALDIR . $name . ".serial";
* 引数 $name のマスターデータキャッシュを生成する.
* 引数 $isDefine が true の場合は, 定数を生成する.
* 定数コメントを生成する場合は, $commentColumn を指定する.
* @param string $name マスターデータ名
* @param array $masterData マスターデータ
* @param bool $isDefine 定数を生成する場合 true
* @param array $commentColumn [0] => キー, [1] => コメント文字列,
[2] => 表示順 を表すカラム名を格納した配列
* @return bool キャッシュの生成に成功した場合 true
function createCache($name, $columns = array(), $isDefine = false,
$commentColumn = array()) {
$path = MASTER_DATA_REALDIR . $name . '.php';
if (!empty($commentColumn)) {
$data .= $this->getMasterDataAsDefine($masterData,
$data .= $this->getMasterDataAsDefine($masterData);
$path = MASTER_DATA_REALDIR . $name . '.serial';
$handle = fopen($path, 'w');
if (fwrite($handle, $data) === false) {
* キャッシュの有無に関係なく, DBからマスターデータを検索し, 取得する.
* 返り値は, key => value 形式の配列である.
* @param string $name マスターデータ名
* @param array $columns [0] => キー, [1] => 表示文字列, [2] => 表示順
if (isset ($columns[2]) && strlen($columns[2]) >= 1) {
$results = $this->objQuery->select($columns[0] . ", " . $columns[1], $name);
// 結果を key => value 形式に格納
foreach ($results as $result) {
$masterData[$result[$columns[0]]] = $result[$columns[1]];
* 引数 $columns が空の場合, デフォルトのカラム名の配列を返す.
* @param array $columns [0] => キー, [1] => 表示文字列, [2] => 表示順
* @return array カラム名を格納した配列
* マスターデータの配列を定数定義の文字列として出力する.
* @param array $masterData マスターデータの配列
* @param array $comments コメントの配列
* @return string 定数定義の文字列
function getMasterDataAsDefine($masterData, $comments = array()) {
foreach ($masterData as $key => $val) {
if (!empty($comments[$key])) {
$data .= "/** " . $comments[$key] . " */\n";
$data .= "define('" . $key . "', " . $val . ");\n";
Documentation generated on Fri, 24 Feb 2012 14:02:32 +0900 by Seasoft
|