首先應該考慮的是:
- 沒有很好的通用模型。每個項目都需要自己的模型。
- 易於閱讀,可管理的代碼
- 不重複相同的代碼(或查詢),所以如果您有某個任務的功能,並且您希望以某種方式訂購它,請修改該功能本身,而不要克隆它
- 使用複雜的數據結構,如數組或對象將數據發送到函數,所以您不必總是修改函數需要的參數
- 資源使用情況。你希望它越多,通用解決方案就會使用更多的資源。
如果我正在選擇,只是在調用函數時可能會影響性能會有多嚴重?可能需要將任何信息拉下來?
這取決於你的網站的負載。大多數時候(如果你不拉大斑點和文本)是可以的,但是當資源稀缺時,你必須指定列。所以你可以節省一些IO時間。
我感覺這使得模型非常臃腫,非常捆綁,創造了大量的重複的代碼。這種模式可能會更好;
也許試試這個:
首先,對於複雜的查詢,我使用這個類我做了很久以前的MySQL。它幫助了很多人。
class sqlAssembler
{
private $data = array();
var $S = array();
var $F = array();
var $W = array();
var $G = array();
var $H = array();
var $O = array();
var $L = array();
//Clause abbreviations
var $clauselist = array
(
'S' => 'SELECT',
'F' => 'FROM',
'W' => 'WHERE',
'G' => 'GROUP BY',
'H' => 'HAVING',
'O' => 'ORDER BY',
'L' => 'LIMIT'
);
//Default clause separators
var $clausesep = array
(
'S' => ',',
'F' => ',',
'W' => ' AND ',
'G' => ',',
'H' => ' AND ',
'O' => ',',
'L' => ''
);
function gen()
{
$tmp = '';
foreach ($this->clauselist as $area => $clause)
{
if (count($this->{$area}))
{
$tmp .= ($clause != 'S' ? ' ' : '') . $clause . ' ';
for ($i=0; $i < count($this->{$area}); $i++)
{
//echo $area = (string)$area;
$tmp .= $this->{$area}[$i];
} //for
} //if
} //foreach
return $tmp;
} //function
function genSection($area, $showsection = 0)
{
$tmp = '';
if (count($this->{$area}))
{
for ($i=0; $i < count($this->{$area}); $i++)
{
$tmp .= $this->{$area}[$i];
} //for
} //if
return $tmp;
} //function
function clear()
{
foreach ($this as $area => $v)
{
//We only care about uppercase variables... do not declare any else variable with ALL UPPERCASE since it will be purged
if (ctype_upper($area))
{
if ($area == 'L')
$this->$area = '';
else
$this->$area = array();
} //if
} //foreach
} //function
public function add($area, $str, $criteria = 1, $sep = '#')
{
if ($criteria)
{
if ($sep == '#')
$sep = $this->clausesep[$area];
//Postgres' OFFSET should be set like: $str = '25 OFFSET 0'
//Not very neat I know, but fuck it
if ($area == 'L')
{
$this->{$area} = array();
} //if
//$ref = $this->$area;
$this->{$area}[] = (count($this->$area) ? $sep : '').$str;
return count($this->$area)-1;
} //if
} //function
public function del($area,$index)
{
if (isset($this->{$area}[$index]))
unset($this->{$area}[$index]);
else
trigger_error("Index nr. {$index} not found in {$area}!",E_USER_ERROR);
} //function
//-*-* MAGIC CHAIN FUNCTIONS
public function S($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function F($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function W($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function G($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function H($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function O($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
public function L($str,$criteria = 1,$sep = '#')
{
$this->add(__FUNCTION__,$str,$criteria,$sep);
return $this;
} //function
} //_sql
也許試試這個:
function getShoppingCart($d)
{
$xx = new sqlAssembler();
$xx->S('*')->
F('items')->
//Notice, that we specified a criteria... if $d['id_item'] exists it will be joined to the WHERE clause, if not it will be left out
W("(id_item > '{$d[id_item]}')",$d['id_item'])->
//Same here
O("dt DESC",$d['date'])
$sql = echo $xx->gen();
//id_item = 11, date = 2009-11-12
//$sql = "SELECT * FROM items WHERE (id_item > '11') ORDER BY dt DESC";
//id_item = null, date = null
//$sql = "SELECT * FROM items";
$data = sqlArray($sql);
//... handle data
}