CakePHP删除模型后打印信息


CakePHP print information from Model after delete

我想设置一条消息,为用户打印确认信息,即:

"感谢您删除XYZ".

现在我的delete()方法是这样的:
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>";
        }
    }

然而,print_r()语句显示了所有产品,并且缺少了一个delete—我认为这是正确的行为。我怎样才能得到刚刚删除的项目的名称?

假设您的$id不是空的,其中包含一些产品

所以你可以使用下面的查询语句

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

那么在$product['Product']['name']中你会得到当前的产品名称也就是你要删除的

如果我还能帮助你,请告诉我