- function BaseModel($params, $db)
-
{ -
$this->db =& $db; -
!$this->alias && $this->alias = $this->table; -
$this->_prefix = DB_PREFIX; -
$this->table = $this->_prefix . $this->table; -
if (!emptyempty($params)) -
{ -
foreach ($params as $key => $value) -
{ -
$this->$key = $value; -
} -
} -
}
大家已经看出$_relation 中间是此实体的关联信息,然后在BaseModel类中的一个函数:
- function _getJoinString($relation_info)
-
{ -
switch ($relation_info['type']) -
{ -
case HAS_ONE:// -
$model =& m($relation_info['model']); -
-
-
$ext_limit = ''; -
$relation_info['ext_limit'] && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit']); -
-
-
$refer_key = isset($relation_info['refer_key']) ? $relation_info['refer_key'] : $this->prikey; -
-
-
return " LEFT JOIN {$model->table} {$model->alias} ON {$this->alias}.{$refer_key}={$model->alias}.{$relation_info['foreign_key']}{$ext_limit}"; -
break; -
case BELONGS_TO: -
-
$model =& m($relation_info['model']); -
$be_related = $model->getRelation($relation_info['reverse']); -
if (emptyempty($be_related)) -
{ -
-
$this->_error('no_reverse_be_found', $relation_info['model']); -
-
return ''; -
} -
$ext_limit = ''; -
!emptyempty($relation_info['ext_limit']) && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit'], $this->alias); -
-
$refer_key = isset($be_related['refer_key']) ? $be_related['refer_key'] :$model->prikey ; -
-
-
return " LEFT JOIN {$model->table} {$model->alias} ON {$this->alias}.{$be_related['foreign_key']} = {$model->alias}.{$refer_key}{$ext_limit}"; -
break; -
case HAS_AND_BELONGS_TO_MANY: -
-
$malias = isset($relation_info['alias']) ? $relation_info['alias'] : $relation_info['middle_table']; -
$ext_limit = ''; -
$relation_info['ext_limit'] && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit'], $malias); -
return " LEFT JOIN {$this->_prefix}{$relation_info['middle_table']} {$malias} ON {$this->alias}.{$this->prikey} = {$malias}.{$relation_info['foreign_key']}{$ext_limit}"; -
break; -
} -
} -
- define('HAS_ONE', 1);
//一对一关联 - define('BELONGS_TO', 2);
//属于关联 - define('HAS_MANY', 3);
//一对多关联 - define('HAS_AND_BELONGS_TO_MANY', 4);
//多对多关联 - define('DROP_CONDITION_TRUNCATE', 'TRUNCATE');
//清空
从这个函数中,我们可以看到,对于不同的关联关系,它会返回不同的关联时的查询语句片断,然后连接上主sql语句,就可以针对实体的关联实体进行相应的关联操作了。
具体操作例子:
- //物品表的操作:
- $model_goods = & m('goods');
- $goods_info = $model_goods->find(array(
-
'conditions' => "if_show=1 and closed=0", -
'fields' => 'goods_id,goods_name,s.store_id,s.store_name', -
'join' => 'blongs_to_store' - ));
这里的'join' => 'blongs_to_store' ,我们从上面的:
- // 一个商品只能属于一个店铺
-
'belongs_to_store' => array( -
'model' => 'store', -
'type' => BELONGS_TO, -
'foreign_key' => 'store_id', -
'reverse' => 'has_goods', -
),
这里我们可以知道这是在与store表进行关联查找了。
到这里,读者就可以知道,如果在上面进行二次开发的话,怎样进行数据库操作就已经很明确的了。
在BaseModel与cls_mysql(mysql.php)中,有很多的有关数据操作的函数,这里就不需要再一一进行解释了,而在cls_mysql中,有一些更基础的操作函数,还有仿真 Adodb 的函数,可以直接跳过BaseModel中的函数
以上介绍了如何在ecmall的平台上进行数据库操作,如果操作更加的复杂,这里还有一种更加直接的方法:
- $sql = "select g.goods_id,g.goods_name, from ".DB_PREFIX."goods g, ".DB_PREFIX."goods_spec gs , ".DB_PREFIX."store s where cate_id='".$cate_id."' AND g.if_show = 1 AND g.closed = 0 and g.goods_id=gs.goods_id and g.store_id=s.store_id and gs.stock>0 and s.state=1 order by g.add_time desc limit 6";
-
$goods_mod =& m('goods'); -
$category_goods = $goods_mod->getAll($sql); -
if(!$category_goods){ -
$category_goods=array(); -
} -
return $category_goods;
就可以直接使用sql语句进行数据操作了。
还可以在BaseModel中定义自己的操作方法,其中可以使用$this->db->(cls_mysql中定义的方法) 来调用cls_mysql中的函数,从而可以添加更加复杂的数据操作函数。