codeigniter


Obtain data through relation table in Codeigniter 3


I have this model below:
<?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));
}
}
?>
I can obtain an array of Person_model via list_persons(). However, in the DB schema, I have the following tables:
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
Sample data is as follow:
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
How can I obtain the information in rel_person_contact in list_persons() function?
It would be best if I can access the contact information like this:
$people = $this->person_model->list_persons();
foreach($people as $person) {
echo 'First Name: ' . $person->first_name . '<br />';
echo 'Emails: ' . implode(',', $person->emails)e . '<br />'; // where $emails is the array containing johndoe#example.com & johndoe#example.com
echo 'Address: ' . $person->address[0]e . '<br />'; // obtaining first address
}
How should I modify the list_persons() function?

Related Links

CodeIgniter Undefined Property Error
Loading and using models in opencart
codeigniter session values are overwritten
CodeIgniter, CI-Merchant and Paypal Sandbox
Calling method on object in CodeIgniter
CodeIgniter Dropdown from query
Best way to deal with image thumbnails on news web site (custom CMS based on codeigniter)?
Codeigniter Flashdata Is Not Showing
Generate XML file from database - Codeigniter
base_url codeigniter solution
How to validate duplicate entries before inserting to database - Codeigniter
CodeIgniter Remove Function From URL
How to validate data before an insert, on multiple records with Codeigniter
Logout Link not working
How to call my model or controller of codeigniter outside application directory?
How to get checkbox checked value from datagrid in codeigniter

Categories

HOME
flutter
opencv
vb.net
logstash
gwt
tags
rtmp
design
anylogic
gluu
jbpm
google-analytics-api
librsvg
bluej
sonata-admin
html5-video
qualtrics
afnetworking-2
zap
stm32f4discovery
django-autocomplete-light
centroid
semantics
jcrop
digital-logic
padding
mathdotnet
esri
toolbar
flexjs
arduino-esp8266
android-imageview
mobilefirst-bluemix
codeskulptor
querydsl
powerbuilder-conversion
jetty-9
jupyter-irkernel
jsonstore
procobol
jpype
angularjs-routing
android-studio-2.1
gulp-babel
ietf-netconf
android-identifiers
jce
glumpy
findall
ultratree
cufft
finalcut
nuitka
restbed
schematiq
asp.net-webpages
cucumber-java
win32com
relaxng
parsoid
openejb
spl
mailx
vbo
google-swiffy
genexus-gam
wintersmith
newsql
slick2d
servlet-listeners
bit.ly
bitrock
brooklyn
citymaps
mouseenter
jquery-backstretch
dataformat
windward
modalpopupextender
pligg
browserid
timestamp-with-timezone
android-actionbar-compat
ribbon-control
xml-dtd
obout
ed
expressionvisitor
closest
staging
paredit
.net-1.1
arraycopy
ryz
queryanalyzer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App