TP5关联模型 一对多 字段别名 easyui tree

一、模型

1.生产计划。包括年月日 

class Plan extends Model
{
    protected $table = 'tplan';
    protected $pk = 'rd';
    public function children() { //建立一对多关联
        return $this->hasMany('PlanProduct','plan_id','rd');->field('rd,plan_id,plan_id  as id, product_id as text');//必须包含主键rd
    }//end function children
}
2.生产计划中的产品。需要生产的产品列表

class PlanProduct extends Model
{
    protected $table = 'tplanproduct';
    protected $pk = 'rd';

    public function children() {

        //建立一对一关联 让被关联的字段附在主表行上返回
        return $this->hasOne('Product','id','product_id')->bind([
                'text' =>'name',
                'id',
            ]);
    }//end function children
}

3.产品表

class Product extends Model
{
    protected $table = 'tproduct';
    protected $pk = 'rd';
}

二、控制器调用  

$list = Plan::with(['children','children.children'])
      ->Distinct(true)->field('nweek  as id, nweek  as text,nyear,nmonth,rd')
      ->order(['nyear'=>'desc','nmonth'=>'desc','nweek'=>'desc'])
      ->where(['nyear'=>'2019','nmonth'=>'3'])
      ->select();
      // dump($list);
      echo $list->toJson();

三、在浏览器上显示的结果

array(2) {
  [0] => array(6) {
    ["id"] => int(2)
    ["text"] => int(2)
    ["nyear"] => int(2019)
    ["nmonth"] => int(3)
    ["rd"] => int(7)
    ["children"] => array(0) {
    }
  }
  [1] => array(6) {
    ["id"] => int(1)
    ["text"] => int(1)
    ["nyear"] => int(2019)
    ["nmonth"] => int(3)
    ["rd"] => int(6)
    ["children"] => array(2) {
      [0] => array(9) {
        ["rd"] => int(1)
        ["plan_id"] => int(6)
        ["product_id"] => string(2) "01"
        ["order_quantity"] => int(10)
        ["order_time"] => int(0)
        ["start_time"] => int(0)
        ["end_time"] => int(0)
        ["text"] => string(5) "QTZ63"
        ["id"] => string(2) "01"
      }
      [1] => array(9) {
        ["rd"] => int(2)
        ["plan_id"] => int(6)
        ["product_id"] => string(2) "02"
        ["order_quantity"] => int(0)
        ["order_time"] => int(0)
        ["start_time"] => int(0)
        ["end_time"] => int(0)
        ["text"] => string(3) "120"
        ["id"] => string(2) "02"
      }
    }
  }
}

[{"id":2,"text":2,"nyear":2019,"nmonth":3,"rd":7,"children":[]},{"id":1,"text":1,"nyear":2019,"nmonth":3,"rd":6,"children":[{"rd":1,"plan_id":6,"product_id":"01","order_quantity":10,"order_time":0,"start_time":0,"end_time":0,"text":"QTZ63","id":"01"},{"rd":2,"plan_id":6,"product_id":"02","order_quantity":0,"order_time":0,"start_time":0,"end_time":0,"text":"120","id":"02"}]}]

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值