在 codeigniter 中将变量从模型传递到模型
我需要将一个变量传递给模型,该模型需要发送另一个变量并使用该变量来查询不同的模型。
EG:
我有一个product_ID,我将其发送到产品模型,从中我找到了supplier_ID。我想将该供应商 ID 获取到供应商模型以获取供应商名称。
你如何在 codeigniter 中实现这个?
I need to pass a variable to model, that model needs to send another back and use that variable to query a different model.
EG:
I have a product_ID which I send to the product model, From that I find out the supplier_ID. I want to grab that supplier_ID to the supplier model to get the supplier name.
How do you implement this in codeigniter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通常会在我的控制器中做这样的事情:
I would generally do something like this in my controller:
我会在两个表之间进行联接并执行一个查询。
I would do a join between the two tables and do one single query.
最简单的方法是将其设置为公共变量(请参阅可见性 在 OOP 中):
如果你真的想在 CI 中这样做,你可以在你的控制器中声明 $product_id 以使其真正成为全局的,但为了整洁我不会亲自这样做。
Easiest way is to set it as a public variable (see visibility in OOP):
If you really wanted to in CI you could declare $product_id in your controller to make it truely global but I wouldn't do that personally for tidyness.
就像坎普所说,听起来加入将是你最好的选择:
当然,如果你有理由将模型分开,这不会有帮助。
Like kemp said, it sounds like a join would be your best bet:
Of course, if you have a reason to keep the models separate, this won't help.
为什么这样的事情对你不起作用?
对我来说似乎很简单。或者,也许您可以澄清这种方法没有达到您的需要?
Why wouldn't something like this work for you?
Seems very straightforward to me. Or, perhaps you could clarify which this approach doesn't achieve what you need?