cakephp模型:如何在模型函数中显示消息

发布于 2024-12-22 16:16:02 字数 826 浏览 3 评论 0原文

MerryParent 模型中的以下函数返回 $merry_parent_id 或空字符串(如果找不到)。如果它要返回一个空字符串,我想停止它并在模型本身中显示错误消息,而不是在控制器中创建 if then else stmt 并在那里显示错误消息。我怎样才能做到这一点?

我不知道如何在模型函数中显示错误消息。在控制器中,我知道我可以使用 $this->Session->setFlash('my error msg')。但这在这里不起作用。

顺便说一句,我正在尝试遵守“胖模型瘦控制器方法”。 :)

class MerryParent extends AppModel{
    //relationships are displayed here
    //form field validations are displayed here

    function getMerryParentId($email){
        $merry_parent_id=$this->field('id',array('MerryParent.email'=>$email));
        return $merry_parent_id;
                /*instead as return $merry_parent_id, I want
                if ($merry_parent_id!='')
                   return $merry_parent_id;
                else
                   //display error message here. 
    }
}

谢谢。

The following function in MerryParent model returns $merry_parent_id or empty string if it is not able to find any. If it is going to return an empty string, i want to stop that and display an error message in the model itself instead of creating an if then else stmt in controller and displaying the error message there. How can I do that?

I don't know on how to display error msgs in model function. In controller I know that I can use $this->Session->setFlash('my error msg'). But that doesn't work here.

By the way, i'm trying to abide by 'fat model thin controller approach'. :)

class MerryParent extends AppModel{
    //relationships are displayed here
    //form field validations are displayed here

    function getMerryParentId($email){
        $merry_parent_id=$this->field('id',array('MerryParent.email'=>$email));
        return $merry_parent_id;
                /*instead as return $merry_parent_id, I want
                if ($merry_parent_id!='')
                   return $merry_parent_id;
                else
                   //display error message here. 
    }
}

thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-12-29 16:16:02

您不应该通过模型显示错误。如果你想显示一些东西只是为了测试目的,你可以在模型中 debug() 等等,但一般来说,你应该使用普通的 MVC 结构,并使用模型来检索数据,使用控制器来处理它,以及显示它的视图。

“胖模型/瘦控制器”作为指导方针很值得遵循,但是当您遵循它太远并停止遵循更重要的 MVC 结构时,这不是一件好事。它并不意味着“胖模型/控制器”。 :)

You shouldn't be displaying errors via the model. If you want to display something just for testing purposes, you can debug() it in the model...etc, but in general, you should use the normal MVC structure, and use the model to retrieve the data, use the controller to process it, and the view to display it.

The "Fat model / Skinny controller" thing is great to follow as a guideline, but when you follow it too far and stop following the more-important MVC structure, it's not a good thing. It's not meant to be "Fat model / Empty controller". :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文