我在下面有这个模型:
<?php
class Person_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function list_persons() {
$query = $this->db->get('persons');
return $query->result(get_class($this));
}
}
?>
我可以通过Person_model
获取list_persons()
数组。但是,在数据库架构中,我有以下表格:
Table: persons
==============
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
first_name VARCHAR NOT NULL
last_name VARCHAR NOT NULL
Table: rel_person_contact
=========================
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
person_id INT NOT NULL
contact_type INT NOT NULL
contact_value VARCHAR NOT NULL
Table: contact_types
====================
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
type_name VARCHAR NOT NULL
示例数据如下:
Table: persons
==============
id first_name last_name
1 John Doe
Table: rel_person_contact
=========================
id person_id contact_type contact_value
1 1 1 johndoe@example.com
2 1 2 +1 12345678
3 1 1 johndoe2@example.com
4 1 4 1 Infinite Loop, CA
Table: contact_types
====================
id type_name
1 Email
2 Work Phone
3 Mobile Phone
4 Address
如何在rel_person_contact
函数中的list_persons()
中获取信息?
最好能访问这样的联系信息:
$people = $this->person_model->list_persons();
foreach($people as $person) {
echo 'First Name: ' . $person->first_name . '<br />';
echo 'Emails: ' . implode(',', $person->emails) . '<br />'; // where $emails is the array containing johndoe@example.com & johndoe@example.com
echo 'Address: ' . $person->address[0] . '<br />'; // obtaining first address
}
我应该如何修改list_persons()
功能?