zoukankan      html  css  js  c++  java
  • TP中关联模型的使用

    class BFinanceProduct extends Common
    {
    protected $pk = 'finance_product_id';
    /**
    * 关联机构
    */
    public function organs()
    {
    return $this->hasOne('BFinanceOrgan','organ_id','finance_organ_id');
    }

    /**
    * @desc 属性、属性值
    */
    public function attrValue(){
    return $this->belongsToMany('BFinanceAttrValue','BFinanceProductTypeAttr','finance_attr_value_id','finance_product_id');
    }

    /**
    * 金融类型
    */
    public function type()
    {
    return $this->hasOne('BFinanceType', 'finance_type_id', 'finance_type_id');
    }
    /**
    * @param $where
    * @param string $field
    * @desc 根据条件获取金融产品数据
    * @date 2020/5/26
    */
    public function getFinanceProductByCondition($where,$field='*',$order='sort desc'){
    $map=[];
    $map[] = ['is_del','=',0];
    $data = $this->getList($map,$field,$order);
    if(!$data->isEmpty()){
    $data->load(['type','organs','attrValue'=>['attrName']]);
    }
    return $data;
    }

    /**
    * @param $where
    * @desc 获取金融产品详情、关联数
    */
    public function getInfoByCondition($where){
    $info = $this->with(['organs','type','attrValue'=>['attrName']])->where($where)->find();
    if($info){
    $this->formatdata($info);
    return $info;
    }else{
    return null;
    }
    }

    }

    用金融产品的模型类作为例子:
    $data = $this->getList($map,$field,$order);
    查询列表的时候,每条数据对应的机构、分类都是一对一的关系所以用法如下
    $data->load(['organs','type']);

    对应的属性和属性值是多对多的关系,所以写法如下

    $data->load(['attrValue'=>['attrName']]);

    其中 type、organs、attrValue 方法定义了他们之前的关系,以及关联条件,避免了平时我们取出数据对数据关系的处理。


    其中注意isEmpty()的用法
    1.{} 2.[]
    只有第二种才能使用改方法判断。

    By:jff
  • 相关阅读:
    JS笔记009
    JS笔记008
    JS笔记007
    JS笔记006
    JS笔记005
    JS笔记004
    JS笔记003
    JS笔记001
    CSS3笔记012
    expdp SYNONYM of publick and schema owner
  • 原文地址:https://www.cnblogs.com/widgetbox/p/jiaofeifei.html
Copyright © 2011-2022 走看看