首先,为了解决您的问题,IMO至少应该尝试将代码封装在几个函数中,您可以为每一步调用它。
为学生选课建议业务规则:
- 被允许创建两个提案的学生。 学生无法为两个提案(DB规则)选择相同的讲师。
讲师只能接受两个学生提议(每个提议将是不同的学生)。这里
发行你的规则有歧义,所以我提出一个假设,该提案表可以有更多的提案超过两个可分配到相同的讲师,但是每一个提案一定是不同的stuID。我假设你在该表中有一个名为'is_proposal_accepted'的列,当讲师接受一个提议时,它被设置为true。
学生在创建新建议时,如果该讲师已经“接受”了两个建议或者学生选择了以前的建议中的讲师,他就不能选择讲师。
第一步 强制数据库中的业务规则的数据完整性要求不能在代码:
ALTER TABLE `proposal` ADD UNIQUE `unique_index`(`stuID`, `lecID`);
执行上述使用命令行或工具,数据库作为海蒂SQL。
第二步封装你的代码(有些非常粗糙的伪代码,以帮助您结构):
class Proposal
{
private $dataArr = [];
private $response = [];
public function doit($postArrayData)
{
//Clean up data if there are issues reject
if (!$this->clean_up_data($postArrayData)) return $this->get_response();
/**
* Before even trying to do any data inserts check the business rules
* Check if the lecturers
*/
if (!$this->check_if_lecturer_is_open()) return $this->get_response();
if (!$this->check_if_student_has_available_proposals()) return $this->get_response();
/**
*
If you have reached here in the method your Application based Business rules have been achieved
NOW save the reocord, if the data violates the
DB Rules your response will be set to an error,
otherwise it will show a success message
*/
$this->add_proposal();
return $this->get_response();
}
public function clean_up_data($postArrayData)
{
/**
* Apply any validation checks for data quality here then assign to array
EXAMPLE: Set each field parameter like so:
*/
$this->dataArr['stuID'] = $postArrayData['stuID'];
if (DataHasIssues) {
$this->set_response(0, "Crappy Data try again");
return false;
}
return true;
}
//Keep this method private, we want our POST data to be processed first and the in
private function add_proposal()
{
/**
*
*
*
* Your DB calls here.....
*
* $query = "INSERT INTO proposal (lecName,lecID,stuName,stuID,proposalTitle,proposalObjective,proposalDesc)
* VALUES (:proposalID,:lecName,:lecID,:stuName,:stuID,:proposalTitle,:proposalObjective,:proposalDesc)";
*/
$query_params = $this->dataArr;
/**
* Execute query with error control here.....
* if(ExecuteQuery){
* * return true; //Student proposal has been added
*}
* set errors here
*/
$this->set_response(0, DB_errors_msg);
return false;
}
private function set_response($success, $message)
{
$this->response["success"] = $success;
$this->response["message"] = $message;
}
public function get_response()
{
return $this->response;
}
public function check_if_lecturer_is_open()
{
/**
*
* $countOfAcceptedProposals ... Select SQL code here to count if the lecID has had two proposals flagged as accepted
* */
if (CountOfAccepptedProsalsByLecID < 2) {
return true;
}
$this->set_response(0, "This Lecturer is full and is not available");
return false;
}
public function check_if_student_has_available_proposals()
{
//USE the dataArr to get the StuID you want to check
$CountOfProsalsByStuID = returned_count;// select count from where stuID = $this->dataArr['stuID'];
/**
*
* $countOfStudentProposals ... Select SQL code here to count if the lecID has had two proposals flagged as accepted
* */
if ($CountOfProsalsByStuID < 2) {
return true;
}
$this->set_response(0, "Student has reached the maximum number of proposals allowed");
return false;
}
}
现在你可以调用的议案类并将它做检查业务规则的辛勤工作并妥善保存你的学生建议。
$proposal = new Proposal();
echo json_encode($proposal->doit($_POST)); //This returns your response object
您正在检查是否存在超过两次的任何lecId,我认为您必须添加一个条件来检查您要插入的特定lecid。 查询结果应该是 select count(*),lecid from lecid = $ _POST ['lecID'] group by lecid count count(*)> 2 –
您需要检查计数的返回值,而不是整行。 – Shadow
不工作:(@HariramanRadhakrishnan – Lawrence