ecmall 数据库关系模型的实现2

Php代码 复制代码
  1. function BaseModel($params, $db)   
  2.      {   
  3.         $this->db =& $db;   
  4.          !$this->alias && $this->alias = $this->table;   
  5.         $this->_prefix = DB_PREFIX;   
  6.         $this->table = $this->_prefix . $this->table;   
  7.         if (!emptyempty($params))   
  8.          {   
  9.             foreach ($params as $key => $value)   
  10.              {   
  11.                 $this->$key = $value;   
  12.              }   
  13.          }   
  14.      

大家已经看出$_relation 中间是此实体的关联信息,然后在BaseModel类中的一个函数:

Php代码 复制代码
  1. function _getJoinString($relation_info)   
  2.      {   
  3.         switch ($relation_info['type'])   
  4.          {   
  5.             case HAS_ONE://   
  6.                 $model =& m($relation_info['model']);   
  7.   
  8.                   
  9.                 $ext_limit = '';   
  10.                 $relation_info['ext_limit'] && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit']);   
  11.   
  12.                   
  13.                 $refer_key = isset($relation_info['refer_key']) ? $relation_info['refer_key'] : $this->prikey;   
  14.   
  15.                   
  16.                 return " LEFT JOIN {$model->table} {$model->alias} ON {$this->alias}.{$refer_key}={$model->alias}.{$relation_info['foreign_key']}{$ext_limit}";   
  17.             break;   
  18.             case BELONGS_TO:   
  19.                   
  20.                 $model =& m($relation_info['model']);   
  21.                 $be_related = $model->getRelation($relation_info['reverse']);   
  22.                 if (emptyempty($be_related))   
  23.                  {   
  24.                       
  25.                     $this->_error('no_reverse_be_found', $relation_info['model']);   
  26.   
  27.                     return '';   
  28.                  }   
  29.                 $ext_limit = '';   
  30.                  !emptyempty($relation_info['ext_limit']) && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit'], $this->alias);   
  31.                   
  32.                 $refer_key = isset($be_related['refer_key']) ? $be_related['refer_key'] :$model->prikey ;   
  33.   
  34.                   
  35.                 return " LEFT JOIN {$model->table} {$model->alias} ON {$this->alias}.{$be_related['foreign_key']} = {$model->alias}.{$refer_key}{$ext_limit}";   
  36.             break;   
  37.             case HAS_AND_BELONGS_TO_MANY:   
  38.                   
  39.                 $malias = isset($relation_info['alias']) ? $relation_info['alias'] : $relation_info['middle_table'];   
  40.                 $ext_limit = '';   
  41.                 $relation_info['ext_limit'] && $ext_limit = ' AND ' . $this->_getExtLimit($relation_info['ext_limit'], $malias);   
  42.                 return " LEFT JOIN {$this->_prefix}{$relation_info['middle_table']} {$malias} ON {$this->alias}.{$this->prikey} = {$malias}.{$relation_info['foreign_key']}{$ext_limit}";   
  43.             break;   
  44.          }   
  45.      }   
  46.   
  47. define('HAS_ONE', 1);                     //一对一关联   
  48. define('BELONGS_TO', 2);                  //属于关联   
  49. define('HAS_MANY', 3);                    //一对多关联   
  50. define('HAS_AND_BELONGS_TO_MANY', 4);     //多对多关联   
  51. define('DROP_CONDITION_TRUNCATE', 'TRUNCATE');  //清空  

从这个函数中,我们可以看到,对于不同的关联关系,它会返回不同的关联时的查询语句片断,然后连接上主sql语句,就可以针对实体的关联实体进行相应的关联操作了。

具体操作例子:

Php代码 复制代码
  1. //物品表的操作:   
  2. $model_goods = & m('goods');   
  3. $goods_info = $model_goods->find(array(   
  4.                 'conditions' => "if_show=1 and closed=0",   
  5.                 'fields'          => 'goods_id,goods_name,s.store_id,s.store_name',   
  6.                 'join'               => 'blongs_to_store'  
  7. ));   

这里的'join' => 'blongs_to_store' ,我们从上面的:

Php代码 复制代码
  1. // 一个商品只能属于一个店铺   
  2.         'belongs_to_store' => array(   
  3.             'model'          => 'store',   
  4.             'type'           => BELONGS_TO,   
  5.             'foreign_key'    => 'store_id',   
  6.             'reverse'        => 'has_goods',   
  7.          ),  

这里我们可以知道这是在与store表进行关联查找了。
到这里,读者就可以知道,如果在上面进行二次开发的话,怎样进行数据库操作就已经很明确的了。
在BaseModel与cls_mysql(mysql.php)中,有很多的有关数据操作的函数,这里就不需要再一一进行解释了,而在cls_mysql中,有一些更基础的操作函数,还有仿真 Adodb 的函数,可以直接跳过BaseModel中的函数
以上介绍了如何在ecmall的平台上进行数据库操作,如果操作更加的复杂,这里还有一种更加直接的方法:

Php代码 复制代码
  1. $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";   
  2.         $goods_mod =& m('goods');   
  3.             $category_goods = $goods_mod->getAll($sql);   
  4.             if(!$category_goods){   
  5.                 $category_goods=array();   
  6.              }   
  7.             return $category_goods;  

就可以直接使用sql语句进行数据操作了。

还可以在BaseModel中定义自己的操作方法,其中可以使用$this->db->(cls_mysql中定义的方法) 来调用cls_mysql中的函数,从而可以添加更加复杂的数据操作函数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值