如何将信息从一个模型调用到另一个Codeigniter

时间:2019-01-27 13:30:39

标签: codeigniter model-view-controller codeigniter-3

我一时被卡住了。无法弄清。我干了文件,尝试了几段视频并尝试了10种不同的方式,但没有任何效果,所以我只有一个视图/模型,在此示例中,目的地和我为优惠提供了单独的文件。两者的控制器都在一个文件中。我也希望目的地中的信息也可以转到优惠。请帮助我不知道我在做什么失踪: 因此,这是最重要的部分:

destination_model.php

<?php class Destination_model extends CI_Model

{
        public function getDestinationDetails($slug) {

        $this->db->select('
            id,
            title,
            information
        ');
        $this->db->where('slug', $slug);
        $query =  $this->db->get('destinations')->row();


        if(count($query) > 0)
        {
            return $query;
        }
        else
        {
            // redirect(base_url());
        }
}

public function getOffersByDestination($destination_id)
{

    $this->db->select('
            o.short_title,
            o.price,
            o.currency,
            o.information,
            o.long_title_slug,
            oi.image,
            c.slug as parent_category
    ');

    $this->db->from('offers o');
    $this->db->join('offers_images oi', 'oi.offer_id = o.id', 'left');
    $this->db->join('categories c', 'c.id = o.category');
    $this->db->group_by('o.id');
    $this->db->where('o.destination', $destination_id);
    $this->db->where('o.active', '1');
    return $this->db->get();

} }

然后在要约的控制器中输入:

$this->load->model('frontend/destination_model');
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);

我所需要的只是标题和目的地信息。 这是优惠的整个控制者:

                            $data = $this->slugs_model->getOfferDetails(strtok($this->uri->segment(2), "."));

                $this->load->model('frontend/offers_model');
                                                                                $this->load->model('frontend/destination_model');
                $this->params['main']       = 'frontend/pages/offer_details';

                $this->params['title']      = $data->long_title;
                $this->params['breadcumb']  = $this->slugs_model->getSlugName($this->uri->segment(1));
                $this->params['data']       = $data;
                $this->params['images']     = $this->slugs_model->getOfferImages($data->id);
                $this->params['similar']    = $this->slugs_model->getSimilarOffers($data->category, $data->id);
                $this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
                $this->params['offers']     = $this->offers_model->getImportantOffers($data->offers, $data->category, $data->id);  

1 个答案:

答案 0 :(得分:-1)

从模型中获取查询结果后,您需要生成查询结果, 例如:row_array(),此函数返回单个结果行。 这是文档:Generating Query Results 试试这个:

$this->load->model('frontend/destination_model');
$data['destination'] = $this->destination_model->getOffersByDestination($data->id)->row_array();
$this->load->view('view_name', $data);

您认为echo $destination['attribut_name'];, 或者您可以打印该数组以查看其是否有效print_r($destination);