我尝试模拟用户类中的活动记录模式。 在考试中的代码下面显示。
Codice PHP:
//method
public function getUtenti($criteria = NULL ,$value = NULL){
$sql ="SELECT * FROM utenti ";
$sql.="LEFT JOIN tecnici ON tec_idtecnico = idutente ";
if($criteria != "")
$sql.="WHERE ".$criteria;
return $this->db->fetchAll($sql,$value);
}
//metodo db
public function fetchAll($sql, array $params = array()){
return $this->executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
}
//method for get users
foreach($user->getUtenti('flagamm = :flagamm', array('flagamm' => 'x')) as $rk){
echo "<option value=\"".$rk['idutente']."\">".$rk['tec_ragionesociale']."</option>";
}
我对选择方法“getUtenti()"
。
是否正确发送方法变量$criteria
(包含where条件)和$value
(包含build sql的数组值)?
使用$_POST
构建查询时出现的问题。
通过这种方式,我无法控制值首次构建查询...
我脑海中的另一个解决方案是创建一个obj $user
,使用每个变量的方法set()
,其中包含变量的所有控件,但是当我调用getUtenti()
时,我不知道如何建立条件......
我不知道如何构建查询。 例如:
$user = new user();
$user->setCompany($_POST['company']);//set the property , in this method i control the leng, make trim ecc ecc.
$set->GetUser(); //this method should give me back all users filters with company
但是如何构建方法GetUser()? 我可以在方法GetUsers()
中这样做$sql = "SELECT * Users ";
if($this->company != "")
$sql.="WHERE company = ".$this->company;
但我无法决定比较运算符=,!=,&lt;,&gt; ecc