我有两种90%相同的方法,即90%的重复代码。我正在尝试扩展第二种方法。
第一种方法:
public function getResultsByID($userID = null){
$sqlParams = array();
if (!$userID)
{
throw new Exception("No User ID Provided");
}
$sqlParams['userID'] = $userID;
$sql = "SELECT t.user_id,
t.owner_id,
t.store_id
FROM users t
LEFT JOIN store s
ON s.store_id = t.store_id
WHERE t.user_id = :userID";
$db = $this->dbWrite : $this->dbRead;
$results = $db->getRow($sql, $sqlParams);
return $results;
}
我的第二种方法非常相同,只是我将多个表连接到此。
public function getMoreResultsByID($userID = null){
$sqlParams = array();
if (!$userID)
{
throw new Exception("No User ID Provided");
}
$sqlParams['userID'] = $userID;
$sql = "SELECT t.user_id,
t.owner_id,
t.store_id
FROM users t
LEFT JOIN store s ON s.store_id = t.store_id
LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
LEFT JOIN owner_detail od ON od.owner_id = t.owner_id
WHERE t.user_id = :userID";
$db = $this->dbWrite : $this->dbRead;
$results = $db->getRow($sql, $sqlParams);
return $results;
}
我无法弄清楚如何从getResultsByID()扩展我的getMoreResultsByID(),以便我可以摆脱相同的代码
提前致谢
答案 0 :(得分:5)
您可以创建另一个私有方法,将SQL查询和userId作为参数传递给它。
<?php
private function queryResults($sql, $userID = null){
$sqlParams = array();
if (!$userID)
{
throw new Exception("No User ID Provided");
}
$sqlParams['userID'] = $userID;
$db = $this->dbWrite : $this->dbRead;
$results = $db->getRow($sql, $sqlParams);
return $results;
}
?>
然后,您可以使用这样的方法。
<?php
public function getResultsByID($userID = null){
$sql = "SELECT t.user_id,
t.owner_id,
t.store_id
FROM users t
LEFT JOIN store s
ON s.store_id = t.store_id
WHERE t.user_id = :userID";
return $this->queryResults($sql, $userID);
}
同样,另一个。
您还可以进一步修改queryResults()
方法,以便为您的班级提供其他方法。
答案 1 :(得分:2)
你不需要添加两个函数来完成相同的工作,你需要做的就是将参数传递给函数
public function getResultsByID($userID = null,$flag){
$sqlParams = array();
if (!$userID)
{
throw new Exception("No User ID Provided");
}
$sqlParams['userID'] = $userID;
if($flag=value1){
$sql = "SELECT t.user_id,t.owner_id,t.store_id
FROM users t
LEFT JOIN store s
ON s.store_id = t.store_id
WHERE t.user_id = :userID";
}elseif($flag=value2){
$sql = "SELECT t.user_id,
t.owner_id,
t.store_id
FROM users t
LEFT JOIN store s ON s.store_id = t.store_id
LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
LEFT JOIN owner_detail od ON od.owner_id = t.owner_id
WHERE t.user_id = :userID";
}
$db = $this->dbWrite : $this->dbRead;
$results = $db->getRow($sql, $sqlParams);
return $results;
}
答案 2 :(得分:0)
您可以将两个参数传递给函数getResultsById
<?php
public function getMoreResultsByID($userID = null, join = TRUE){
$sqlParams = array();
if (!$userID)
{
throw new Exception("No User ID Provided");
}
$sqlParams['userID'] = $userID;
$sql = "SELECT t.user_id,
t.owner_id,
t.store_id
FROM users t";
if (join){
$sql.="LEFT JOIN store s ON s.store_id = t.store_id
LEFT JOIN owner_contact oc ON oc.owner_id = t.owner_id
LEFT JOIN owner_detail od ON od.owner_id = t.owner_id";
}else{
$sql.="LEFT JOIN store s
ON s.store_id = t.store_id";
}
$sql.="WHERE t.user_id = :userID";
$db = $this->dbWrite : $this->dbRead;
$results = $db->getRow($sql, $sqlParams);
return $results;
}