??? AddDepartment adds a new department using the provided name and description. Its ID
is automatically generated by the database (the department_id column in the department
table is an AUTO_INCREMENT column).
??? UpdateDepartment changes a department??™s details. Its parameters are the department??™s
department_id value, its new name, and its new description.
??? DeleteDepartment deletes the department specified by the department_id parameter.
CHAPTER 10 ?– CATALOG ADMINISTRATION: DEPARTMENTS AND CATEGORIES 292
Exercise: Implementing the Business Tier
Now it??™s time to implement these four new methods. Add this code to the Catalog class in business/
catalog.php:
// Retrieves all departments with their descriptions
public static function GetDepartmentsWithDescriptions()
{
// Build the SQL query
$sql = 'CALL catalog_get_departments()';
// Execute the query and return the results
return DatabaseHandler::GetAll($sql);
}
// Add a department
public static function AddDepartment($departmentName, $departmentDescription)
{
// Build the SQL query
$sql = 'CALL catalog_add_department(:department_name,
:department_description)';
// Build the parameters array
$params = array (':department_name' => $departmentName,
':department_description' => $departmentDescription);
// Execute the query
DatabaseHandler::Execute($sql, $params);
}
// Updates department details
public static function UpdateDepartment($departmentId, $departmentName,
$departmentDescription)
{
// Build the SQL query
$sql = 'CALL catalog_update_department(:department_id, :department_name,
:department_description)';
// Build the parameters array
$params = array (':department_id' => $departmentId,
':department_name' => $departmentName,
':department_description' => $departmentDescription);
// Execute the query
DatabaseHandler::Execute($sql, $params);
}
CHAPTER 10 ?– CATALOG ADMINISTRATION: DEPARTMENTS AND CATEGORIES 293
// Deletes a department
public static function DeleteDepartment($departmentId)
{
// Build the SQL query
$sql = 'CALL catalog_delete_department(:department_id)';
// Build the parameters array
$params = array (':department_id' => $departmentId);
// Execute the query and return the results
return DatabaseHandler::GetOne($sql, $params);
}
Implementing the Data Tier
You??™ll add four stored procedures to the data tier that correspond to the four business tier
methods you wrote earlier.
Pages:
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413