ニュースが削除された場合、現在のニュースに関連するアイコン ファイルを削除する必要があります。2つのアプローチがあります。
初め:
public function admin_delete ($id = null, $icon = null) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->News->delete($id)) {
unlink(WWW_ROOT . 'img/icons/news/' . $icon);
$this->Session->setFlash('ok!');
$this->redirect(array('action' => 'index'));
}
}
ビューからこのアクションにレコード ID とファイル名を渡す必要があります。私にとっては少し醜いように見え、Nginx関連の問題を引き起こす可能性もあります.
二つ目:
public function admin_delete ($id = null) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
$icon = $this->News->read('icon', $id);
if ($this->News->delete($id)) {
unlink(WWW_ROOT . 'img/icons/news/' . $icon['icon']);
$this->Session->setFlash('ok!');
$this->redirect(array('action' => 'index'));
}
}
しかし、それが良いアプローチであるかどうかはわかりません。 read
orを使用する必要がありfind('first')
ます。より正しい方法でそれを行う方法についてアドバイスをいただければ幸いです。前もって感謝します!