在 codeigniter 中将变量从模型传递到模型

发布于 2024-09-05 06:38:35 字数 174 浏览 6 评论 0原文

我需要将一个变量传递给模型,该模型需要发送另一个变量并使用该变量来查询不同的模型。

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 技术交流群。

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

发布评论

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

评论(5

想念有你 2024-09-12 06:38:35

我通常会在我的控制器中做这样的事情:

$this->load->model('Products');
$this->load->model('Suppliers');

$result = $this->Suppliers->get_supplier_name($this->Products->get_product_supplier($product_id));

I would generally do something like this in my controller:

$this->load->model('Products');
$this->load->model('Suppliers');

$result = $this->Suppliers->get_supplier_name($this->Products->get_product_supplier($product_id));
夜访吸血鬼 2024-09-12 06:38:35

我会在两个表之间进行联接并执行一个查询。

I would do a join between the two tables and do one single query.

穿透光 2024-09-12 06:38:35

最简单的方法是将其设置为公共变量(请参阅可见性 在 OOP 中):

function blah() {
    public $product_id = 0;

    $this->load->model('Products');

    // Products model can set and access $this->product_id

    $this->load->model('Suppliers');

    // Suppliers model can also set and access $this->product_id
}

如果你真的想在 CI 中这样做,你可以在你的控制器中声明 $product_id 以使其真正成为全局的,但为了整洁我不会亲自这样做。

Easiest way is to set it as a public variable (see visibility in OOP):

function blah() {
    public $product_id = 0;

    $this->load->model('Products');

    // Products model can set and access $this->product_id

    $this->load->model('Suppliers');

    // Suppliers model can also set and access $this->product_id
}

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.

伏妖词 2024-09-12 06:38:35

就像坎普所说,听起来加入将是你最好的选择:

// pass the $product_ID variable to the model
$this->db->select('supplier_tbl.supplier_name');
$this->db->from('supplier_tbl');
$this->db->join('product_tbl','product_tbl.supplier_ID = supplier_tbl.supplier_ID');
$this->db->where('product.tbl',$product_ID);
$query = $this->db->get();

当然,如果你有理由将模型分开,这不会有帮助。

Like kemp said, it sounds like a join would be your best bet:

// pass the $product_ID variable to the model
$this->db->select('supplier_tbl.supplier_name');
$this->db->from('supplier_tbl');
$this->db->join('product_tbl','product_tbl.supplier_ID = supplier_tbl.supplier_ID');
$this->db->where('product.tbl',$product_ID);
$query = $this->db->get();

Of course, if you have a reason to keep the models separate, this won't help.

司马昭之心 2024-09-12 06:38:35

为什么这样的事情对你不起作用?

$s_id = $this->product_model->getSupplierID($p_id);
$s_name = $this->supplier_model->getSupplierName($s_id);

对我来说似乎很简单。或者,也许您可​​以澄清这种方法没有达到您的需要?

Why wouldn't something like this work for you?

$s_id = $this->product_model->getSupplierID($p_id);
$s_name = $this->supplier_model->getSupplierName($s_id);

Seems very straightforward to me. Or, perhaps you could clarify which this approach doesn't achieve what you need?

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