出于学习目的,我正在oop php中针对属性/广告开发cms。我为每个媒体资源/广告和三张桌子都设有照片库。
properties (id, location, price)
property_photo (id, property_id, photo_id)
photos (id, name, extension)
我尝试单击删除属性的按钮删除时,它会自动删除property_photo和连接到这些属性的照片表中的照片,并在我的项目文件夹中删除它们。目前,我能够从属性表和property_photo表中删除属性,但是无法从照片表和项目文件夹中删除那些照片。任何帮助表示赞赏。这是我的代码。
AdModel:
public function deleteProperty($id)
{
$this->db->query('DELETE FROM properties WHERE id=:id');
$this->db->bind(':id', $id);
if ($this->db->execute()) {
return true;
}
else {
return false;
}
}
public function deletePropertyPhoto($id)
{
$this->db->query('DELETE FROM property_photo WHERE property_id=:property_id');
$this->db->bind(':property_id', $id);
$this->db->query('DELETE FROM photos WHERE id=(SELECT photo_id FROM property_photo WHERE property_id=:property_id)');
$this->db->bind(':property_id', $id);
$this->db->execute();
}
AdsController:
public function addeleteAction()
{
$userinfo = $this->Auth->Auth(['admin', 'moderator']);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$this->Auth->isSet($_GET['id'], "ads/index");
if ($this->AdModel->deleteProperty($_GET['id'])) {
$photo = $this->AdModel->deletePropertyPhoto($_GET['id']);
if ($photo != false) {
if (file_exists('public/photos/' . $photo->name . '.' . $photo->extension)) {
unlink('public/photos/' . $photo->name . '.' . $photo->extension);
}
}
redirect('ads/index');
}
echo "Property is not found!!!";
}
}