qeephp 地区条件查询优化 finder传参处理

单行、唯一 0     290      框架   0     0
目的:查询地区下面的所有村寨 条件:村寨表存有省份、地州、区县、乡镇、地区id的外键,都是地区表的主键 思路:接收post的zone_id, 查询该地区信息,使用地区等级level判断是哪一级,然后把zone_id赋值到相应的省份、地州、区县、乡镇、地区id字段上面;

优化过的代码:

//指定要查询的字段(包括模型定义的setter),减少关联
protected $_cols = array('id','name','zone_allname','status');


            if($this->_context->isPOST()){
	        $zone_id = intval($this->_context->zone_id);
	        if($zone_id){
	            unset($_POST['zone_id']);

	            $level = Zone::find('id=' . $zone_id)->setColumns('level')->getOne()->level;

	            if($level==1){
	                //省级province
	                $_POST['province'] = $zone_id;
	            }elseif ($level==2){
	                //地州级prefecture
	                $_POST['prefecture'] = $zone_id;
	            }elseif ($level==3){
	                //县级county
	                $_POST['county'] = $zone_id;
	            }elseif ($level==4){
	                //乡镇town
	                $_POST['town'] = $zone_id;
	            }elseif($level==5) {
	                //村village
	                $_POST['village'] = $zone_id;
	            }else{
	                $_POST['zone_id'] = $zone_id;
	            }
	        }
	    }


开始的代码:

            if (isset($this->_context->zone_id) && intval($this->_context->zone_id) == 0) {
	        unset($_POST['zone_id']);
	    }
	    if(isset($_POST['zone_id'])){
		    $zone_id = intval($this->_context->zone_id);
		    if($zone_id){
		    	$zone = Zone::find('id='.$zone_id)->getOne();
			   	if($zone->level == 1){
			   		$stockades = Stockade::find('province ='.$zone->id)->getAll();
			    }elseif ($zone->level == 2){
			    	$stockades = Stockade::find('prefecture ='.$zone->id)->getAll();
			    }elseif ($zone->level == 3){
			    	$stockades = Stockade::find('county ='.$zone->id)->getAll();
			    	unset($_POST['zone_id']);
			    }elseif ($zone->level == 4){
			    	$stockades = Stockade::find('town ='.$zone->id)->getAll();
			    }elseif ($zone->level == 5){
			    	$stockades = Stockade::find('village ='.$zone->id)->getAll();
			    }else{
			    	$stockades = Stockade::find('zone_id ='.$zone->id)->getAll();
			    }
			    if(count($stockades)){
			    	$ids = Helper_Array::implode($stockades, ',', 'id', 'id');
			    }else{
			    	$ids = "-1";
			    }
			    $_POST[] = "id in (" . $ids . ")";

			    unset($_POST['zone_id']);
			    dump($_POST);
		    }
	    }

 

想说的话:代码一定要尽量写的优雅简洁,且消耗小,速度快

captcha
忘记密码? 注册
第三方登录
微信赞赏
支付宝赞赏