php mysql 模型_mysql+php:实现一个好的模型

首先,你应该考虑的是:

没有好的通用车型。每个项目都需要自己的模型。

易于阅读、可管理的代码

不要重复相同的代码(或查询),因此,如果您有某个任务的函数,并且希望以其他方式对其进行排序,请修改该函数本身,不要克隆它。

使用复杂的数据结构(如数组或对象)向函数发送数据,因此不必总是修改函数所需的参数。

资源使用情况。您越想让它无处不在,通用解决方案将使用越多的资源。

如果我选择*只是因为调用函数可能需要下拉的任何信息,那么性能会受到多大的影响?

这取决于你网站的负载。大多数情况下(如果不提取大的blob和文本)*是可以的,但是当资源不足时,必须指定列。这样可以节省一些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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值