无限星辰工作室-客户无限互联网动力之源

标题: Thinkphp Model 关联模型使用实例 [打印本页]

作者: crx349    时间: 2018-6-29 01:22
标题: Thinkphp Model 关联模型使用实例
实例试用情况:

1.有2个Model 用category_id字段关联
2.模型ProductCollection 需要调用 ProductCategory中的数据


其中:ProductCollection  写法

  1. /**
  2.          * 商品收藏列表
  3.          * @param      $member_list_id
  4.          * @return array
  5.          */
  6.         public static function collection_list($member_list_id) {
  7.                 $where['member_list_id'] = $member_list_id;
  8.                 $order = ['time' => 'DESC'];
  9.                 $field = ['product_id','category_id'];

  10.                 $list = self::paginates($field, $where, $order);

  11.                 if (!$list->isEmpty()) {
  12.                         $list->load(['product','productCategory']);
  13.                         $list->hidden(['product','productCategory']);
  14.                 }

  15.                 return $list->toArray();
  16.         }
复制代码

  1. /**
  2.      * 关联商品规格 一对一
  3.      * @return \think\model\relation\BelongsTo
  4.      */
  5.     public function productCategory()
  6.     {
  7.         $relation = $this->belongsTo(ProductCategory::getModel(),'category_id');
  8.         $relation->where('enable', 1);
  9.         $relation->order('sort', 'ASC');
  10.         return $relation;
  11.     }
复制代码



作者: crx349    时间: 2018-8-2 04:12
  1. // 数据库调试模式
  2.     'debug'          => true,
  3.     // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
  4.     'deploy'         => 0,
  5.     // 数据库读写是否分离 主从式有效
  6.     'rw_separate'    => false,
  7.     // 读写分离后 主服务器数量
  8.     'master_num'     => 1,
  9.     // 指定从服务器序号
  10.     'slave_no'       => '',
  11.     // 是否严格检查字段是否存在
  12.     'fields_strict'  => true,
  13.     // 数据集返回类型 array 数组 collection Collection对象
  14.     'resultset_type' => 'array',
  15.     // 是否自动写入时间戳字段
  16.     'auto_timestamp' => false,
  17.         //自动时间格式转换
  18.         'datetime_format'=>false,
  19.     // 是否需要进行SQL性能分析
  20.     'sql_explain'    => false,
复制代码


多库关联模型必填配置




欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://www.xmspace.net/) Powered by Discuz! X3.4