「七天自制PHP框架」应用:Model外键链接

这里以行政区数据为例:

一级行政区数据范例:

二级行政区范例:

三级行政区范例:

在Model层建立三个Model

class ProvinceModel extends Model{
	public static $data;
	public static $name;
	public function __construct(){
		parent::__construct();
		$this::$name='province_list';//name of obj
		$this::$table='china_province';
		$this::$primary_key='provinceid';
	}
}

class CityModel extends Model{
	public static $data;
	public static $name;
	public function __construct(){
		parent::__construct();
		$this::$name='city_list';//name of obj
		$this::$table='china_city';
		$this::$primary_key='cityid';
	}
}

class AreaModel extends Model{
	public static $data;
	public static $name;
	public function __construct(){
		parent::__construct();
		$this::$name='area_list';//name of obj
		$this::$table='china_area';
		$this::$primary_key='areaid';
	}
}

因为非常反感每次做外键链接时写不完的FOR循环和IF判断,所以做了个HasMany的一对多关系,增强代码复用性

在Controller层绑定关系

$province_model=new ProvinceModel();
$province_model::$data=$province_model::all();
		
$city_model=new CityModel();
$city_model::$data=$city_model::all();
		
$area_model=new AreaModel();
$area_model::$data=$area_model::all();
		
$city_model->HasMany($area_model,'cityid');
$province_model->HasMany($city_model,'provinceid');

最后把数据导出为JSON格式

 

posted @ 2018-08-02 21:44  编程老头  阅读(455)  评论(0编辑  收藏  举报