2014-10-12 2 views
-1

Я изучаю их основной учебник для создания блога. На этапе удаления у меня есть:Настроить setFlash() при удалении сообщения

public function delete($id){ 
    // if($this -> request -> is('get')){ 
    // throw new MethodNotAllowedException(); } 

    if ($this -> Post -> delete($id)) { 
     $this -> Session -> setFlash(
      __('The article %s was deleted', h($id)));//here is the line 
      return $this -> redirect(array('action' => 'index')); 
    } 

} 

Я хотел бы вместо The article ID was deleted получить The article TITLE was deleted;

Мой вопрос, почему приведенный ниже код не работает в этом случае?

__('The article %s was deleted', h($title))); 
+0

Я havnt видел объявление h где угодно? –

+2

Как вы ожидаете, что для работы, нет никакой переменной с именем 'title' в любом месте вашего кода? – ndm

+0

@AvinashBabu, не объявлен. Оригинал можно найти здесь http://book.cakephp.org/2.0/ru/getting-started.html#deleting-posts – ADDA

ответ

1
public function delete($id){ 
    // get the title 
    $this->Post->id = $id; 
    $title = $this->Post->field('title'); 

    // delete the record 
    if ($this->Post->delete($id)) { 
     $this->Session->setFlash(__('The article %s was deleted', h($title))); // output the title 
     return $this -> redirect(array('action' => 'index')); 
    } 

} 
0

так $id передается в качестве аргумента в функции удаления.

Мы можем получить к нему доступ в следующей строке.

__('The article %s was deleted', h($id))); 

Но для доступа другого поля со стола, вы должны извлечь его из базы данных

, используя принятый $id.

Такие, как:

$title = $this->Post->field('title'); 

, а затем использовать его.

Это ваш полный код с изменениями.

public function delete($id){ 

$this->Post->id = $id; 
$title = $this->Post->field('title'); 


if ($this->Post->delete($id)) { 
    $this->Session->setFlash(__('The article %s was deleted', h($title))); 
    return $this -> redirect(array('action' => 'index')); 
} 
Смежные вопросы