自关联查询没有数据

浏览:1133 发布日期:2017/05/02 分类:ThinkPHP5专区 关键字: thinkphp5 关联模型 自关联
地区表结构:CREATE TABLE `actor_zsys_china_area` (
  `area_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT COMMENT '地区id',
  `parent_id` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '地区父id',
  `area_name` varchar(120) NOT NULL DEFAULT '' COMMENT '地区名称',
  `area_type` tinyint(1) NOT NULL DEFAULT '2' COMMENT '地区类型 0:国家,1:省,2:市,3:市辖区/县',
  PRIMARY KEY (`area_id`),
  KEY `parent_id` (`parent_id`),
  KEY `area_type` (`area_type`)
) ENGINE=MyISAM AUTO_INCREMENT=3429 DEFAULT CHARSET=utf8 COMMENT='国家省市县地区信息表;made in actor'

地区model:<?php
namespace app\common\model;

class Zsyschinaarea extends Actor{
    protected $table = 'actor_zsys_china_area';
    protected $pk = 'area_id';

    public function areapk(){
        return $this->hasMany('zsyschinaarea', 'parent_id');
    }

    public function with_areappk($where=[]){
        return $this->with('areapk')->where($where)->select();
    }



    public function areafk(){
        return $this->belongsTo('zsyschinaarea', 'parent_id', 'area_id');
    }

    public function with_areafk($where=[]){
        return $this->with('areafk')->where($where)->select();
    }
}
测试自关联模型的控制器:<?php
namespace app\admin\controller;
use app\common\controller\Commfun;

class Station extends Actor{

    public function index(){        
        $da['area_id'] = input('station_id');
        $s = model('zsyschinaarea')->with_areafk($da);
        echo model('zsyschinaarea')->getLastSql();echo "<br><br>";
        dump($s);
    }

}
输出结果:


单独打印getLastSql语句有数据:



为什么我模型里面的关联预载入方法with_areafk查询处理结果为空呢?单独areafk方法查询结果是正确的。 areapk方法里面查询处理结果也是正确的?
最佳答案
评论( 相关
后面还有条评论,点击查看>>