简体   繁体   English

如何使用 CodeIgniter 3 中的外键从表中获取列数据

[英]How to fetch columns data from a table using the foreign key in CodeIgniter 3

Hi first of all I should tell you that English is not my first language so excuse any misunderstandings.嗨,首先我应该告诉你英语不是我的第一语言,所以请原谅任何误解。 What I'm trying here is to access to the "donors" table data using the foreign key in the "packets" table and I need to get that data in the controller to use.我在这里尝试的是使用“数据包”表中的外键访问“捐助者”表数据,我需要在 controller 中获取该数据以使用。 I refer PacketID in my view and in the staff controller I need to get the specific DonorMobile and DonorName to send an SMS to that donor.我在我的观点和staff controller 中提到了PacketID ,我需要获取特定的DonorMobileDonorName才能向该捐赠者发送 SMS。 I have tried creating multiple model functions but didn't work.我尝试创建多个 model 函数,但没有奏效。 Can it be done by that way or is there any other way?可以通过这种方式完成还是有其他方式? TIA!蒂亚!

Screenshots of donors and packets tables donorspackets表的屏幕截图

donors table and packets table捐助者表数据包表

Staff controller - I know you can't access data like $donorData->DonorID in the controller员工 controller - 我知道您无法访问 controller 中的$donorData->DonorID等数据

public function markAsUsed($packet)
    {
            $this->load->Model('Donor_Model');
            $donorData = $this->Donor_Model->getDonors($packet);

            $this->load->Model('Donor_Model');
            $data = $this->Donor_Model->markAsUsed($donorData->DonorID);

            $sid = 'twilio sid';
            $token = 'twilio token';
            $donorMobile = '+94' . $donorData->DonorMobile;
            $twilioNumber = 'twilio number';

            $client = new Twilio\Rest\Client($sid, $token);
            $message = $client->messages->create(
                $donorMobile, array(
                    'from' => $twilioNumber,
                    'body' => 'Thank you ' . $donorData->DonorName . ' for your blood donation. Your donation has just saved a life.'
                )
            );

            if ($message->sid) {
                $this->load->Model('Donor_Model');
                $this->Donor_Model->changeStatus($data->isAvailable);
                redirect('staff/viewpackets');
}
}

Model Model

function getDonors($packet) {
        $data = $this->db->get_where('packets', array('PacketID' => $packet));
        return $data->row();
    }

function markAsUsed($donor)
    {
        $data = $this->db->get_where('donors', array('DonorID' => $donor));
        return $data->row();
    }

function changeStatus($packet)
    {

        $data = array(
            'isAvailable' => False,
        );

        return $this->db->update('packets', $data, ['PacketID' => $packet]);
    }

Found the answer.找到了答案。 You can do this only using the controller.您只能使用控制器来执行此操作。

$packetData = $this->db->get_where('packets', array('PacketID' => $packet));

            foreach ($packetData->result() as $row) {
                $donorID = $row->DonorID;
                $packetDonatedDate = $row->DonatedDate;
            }

$donorData = $this->db->get_where('donors', array('DonorID' => $donorID));

            foreach ($donorData->result() as $row) {
                $donarName = $row->DonorName;
                $donorMobile = $row->DonorMobile;
            }

What you did in the answer is not advisable, using loops to fetch from tables will slow the system by large when you have large datasets.您在答案中所做的事情是不可取的,当您拥有大型数据集时,使用循环从表中获取会大大降低系统速度。 What was expected is to use JOIN queries to join the donors and packets tables as shown below:预期的是使用 JOIN 查询来连接捐助者和数据包表,如下所示:

$this->db->select('donors_table.DonorName,donors_table.DonorMobile,packets_table.*')->from('packets_table'0;
$this->db->join('donors_table','donors_table.DonorID=packets_table.DonorID');
$query = $this->db->get();
$result = $this->db->result();

Then you can iterate through each donor as below:然后你可以遍历每个捐助者,如下所示:

foreach($result as $row){
   $donarName = $row->DonorName;
   $donorMobile = $row->DonorMobile;
}

NB: It is always advisable to perform any fetch, insert, delete or update operations from the model, that is why Codeigniter is an MVC.注意:始终建议从 model 执行任何获取、插入、删除或更新操作,这就是 Codeigniter 是 MVC 的原因。 Stop fetching data directly from database within Controllers.停止直接从控制器中的数据库获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM