Tamas
Tamas

Reputation: 11214

CakePHP print information from Model after delete

I'd like to set a message that prints a confirmation for the user, i.e.:

"Thank you for deleting XYZ".

At the moment my delete() method looks like this:

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

        if ($this->Product->delete($id)) {
            echo "<pre>";
            echo "id: " . $id;
            print_r($this->Product->find('all',
                array("condition",
                    array("Product.id" => $id)
                    )
                )
            );
            echo "</pre>";
        }
    }

However, the print_r() statement shows all products, and the one delete is missing - which I assume is the right behaviour. How can I get the name of the item just deleted?

Upvotes: 0

Views: 328

Answers (1)

liyakat
liyakat

Reputation: 11853

From assumption your $id is not empty which contain some product

so you can use query like below

$product = $this->Product->find('first', array(
        'conditions' => array(
            'Product.id' => $id,
        ) ,
        'fields' => array(
            'Product.name'
        ) 
    ));

so in $product['Product']['name'] you will get current product name which is you are going to delete

Please let me know if i can assist you more..

Upvotes: 2

Related Questions