Cakephp-根据上下文查询模型

发布于 2024-12-11 21:39:00 字数 143 浏览 0 评论 0原文

我需要做这样的事情。

$model="MyModel";
$results=$this->"MyModel"->find("all);

所以我需要根据情况调用不同的函数。我怎样才能做到这一点?

I need to do something like this.

$model="MyModel";
$results=$this->"MyModel"->find("all);

so I need to call a different function according to the case. How can I accomplish that?

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

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

发布评论

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

评论(3

吐个泡泡 2024-12-18 21:39:00

好吧,您想要做的是根据条件调用不同的模型,对吗?

$model = "Model";
$results = $this->{$model}->find('all');

但是,如果您发现自己需要这样做,可能是因为代码组织不正确。您可能想寻找替代方案。

Well, what you're trying to do is call a different Model according to conditions right?

$model = "Model";
$results = $this->{$model}->find('all');

However, if you find yourself needing to do this, it might be due to organizing your code incorrectly. You might want to look into alternatives.

鼻尖触碰 2024-12-18 21:39:00

您可以在控制器操作中执行此操作

class MyControllerController extends AppController
{
    function action_name()
    {
        $this->uses = array(
            'MyModel',
            'AnotherModel'
        );

        $this->AnotherModel->find('all');
    }
}

You can do that from within the controller action

class MyControllerController extends AppController
{
    function action_name()
    {
        $this->uses = array(
            'MyModel',
            'AnotherModel'
        );

        $this->AnotherModel->find('all');
    }
}
永言不败 2024-12-18 21:39:00

$model = "模型";
$results = $this->{$model}->find('all');

根据 CakePHP 编码约定,使用这种方法是可以的,并且没有更好或更干净的替代方案。特别是当您用复杂的逻辑编写行为时。所以坚持下去,不要担心。

$model = "Model";
$results = $this->{$model}->find('all');

Using this approach is ok in accordance to CakePHP coding conventions and has no better or cleaner alternatives. Especially when you are coding behaviors with complex logic. So stick to it and don't worry.

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