1

我的代码如下

控制器代码:

class statController extends baseController {

public function index() {
    $this->registry->template->projectsWise = $this->selectProjectWise();
    $this->registry->template->show("stat");
}

private static function selectStat($query, $values) {

    $statMapper = new statMapper();
    $select = new common();
    $select->setItems($query);
    $select->setValues($values);
    return $statMapper->select($select);
}

private function selectProjectWise() {
    $date = date("Y-m-d");
    $values = array($date, $date);
    return self::selectStat("project_wise", $values);
}
private function selectLocationWise(){
    $date = date("Y-m-d");
    $values = array($date, $date, 1);
    return self::selectStat("location_wise", $values);
}

映射器代码:

class statMapper extends baseMapper {

private $query = array(
    "project_wise" => "SELECT p.proj_Name, p.proj_Type, p.proj_Category, COUNT(ra.emp_Id) as allocation FROM project AS p, resource_assignment AS ra WHERE p.proj_Start_Date <= '%s' AND p.proj_End_Date >= '%s' AND p.proj_Status = 1 AND ra.proj_Id = p.proj_Id GROUP BY p.proj_Name ",
    "location_wise" => "SELECT e.user_Location, p.proj_Name, p.proj_Type, p.proj_Category, COUNT(ra.emp_Id) as allocation FROM project AS p, resource_assignment AS ra, employee as e WHERE p.proj_Start_Date <= '%s' AND p.proj_End_Date >= '%s' AND p.proj_Status = %d AND ra.proj_Id = p.proj_Id GROUP BY p.proj_Name "
);

public function select($select) {
    $connect = parent::connect();
    $query = sprintf($this->query[$select->getItems()], $select->getValues()[0], $select->getValues()[1]);
    try {
        $result = $connect->query($query);
        if ($result) {
            $table = array();
            while ($row = $result->fetch_object()) {
                $table[] = $row;
            }
            return $table;
            parent::disconnect();
        } else {
            throw (new Exception($connect->error . "<br/>" . $query . "<br/>"));
        }
    } catch (Exception $exp) {
        require_once 'views/error.php';
        exit();
    }
}

}

($query->"location_wise")statController->selectLocationWise()函数中添加了 3 个说明符。当我使用它时,我如何添加$select->getValues()[2]价值statMapper->select(),而不是手动添加。

4

1 回答 1

0

您可以使用http://www.php.net/manual/ru/function.vsprintf.php而不是sprintf将整个数组作为参数传递。

但无论如何,你的代码看起来很糟糕。从 StatMapper 的外部视图来看,既无法识别参数的类型,也无法识别它们的计数。代码将难以维护。

我建议使用数据访问对象模式或学习框架。

于 2013-08-20T20:40:53.507 回答