Cakephp-根据上下文查询模型
我需要做这样的事情。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您想要做的是根据条件调用不同的模型,对吗?
但是,如果您发现自己需要这样做,可能是因为代码组织不正确。您可能想寻找替代方案。
Well, what you're trying to do is call a different Model according to conditions right?
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.
您可以在控制器操作中执行此操作
You can do that from within the controller action
$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.