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 Database result align not proper
Alternative to uri segment on codeigniter?
Dompdf generated pdf is 0 bytes
Codeigniter - cookies do not work in internet explorer 8
how to get hash value from URL by codeigniter
Codeigniter Driver: cannot access to parent method from subclasses
add second database connection
removing backslashes in the query using codeigniter mysql
How to redirect main URL to the sub-directory in codeigniter
Codeigniter upload issue
access folder outside code igniter
have character \ in result of rest service using CodeIgniter
codeigniter post value not available to form. use post value in controller logic
Change group of a newly created User in PyroCMS
Codeigniter pagination create_link() display blank on view page
Cookies not beeing set, codeigniter

Categories

HOME
wpf
odoo-10
music
copy
varnish
outlook
backand
mvvm-light
webdriver-io
sweetalert
google-adwords
freebsd
base
autofac
wso2ei
doorkeeper
union
deserialization
aar
ssrs-tablix
silk-performer
curve-fitting
lexikjwtauthbundle
multiprocessing
dotnetbar
rgdal
google-distancematrix-api
datamatrix
stylesheet
iar
editorconfig
popupwindow
rdf4j
debugview
pmd
charts.js
skyscanner
sqlite.swift
dwm
powerbuilder-conversion
bezier
weebly
sagepay
nomenclature
wso2iot
edeliver
ulimit
tabview
ssms-2012
azure-cdn
manifoldjs
ios-app-group
kony
harvest-scm
playback
hipi
cmsmadesimple
qtcpserver
informatica-cloud
angular2-rc5
topojson
cglib
xpdf
network-traffic
namenode
activemodel
duration
schematiq
id3
lidar
storage-duration
screen-capture
kinto
coldfusion-10
gulp-rename
protected
mailx
iphone-5
horizontalscrollview
information-schema
formhelper
zeroclipboard
notifydatasetchanged
image-scaling
nitrogen
master-theorem
srp
qt4.7
colormatrix
jquery-mobile-popup
xidel
ioerror
libnids
xcache
firebird2.1
sparc
geometry-surface
html5-apps
hamlc
12factor
timespan
clean-urls
browserid
ekeventkit
hpple
ed
semantic-zoom
sdl.net
retain
jquery-pagination
pong
cluetip
machine.config
xoom
custom-protocol
xml-database
markitup
clipboard-pictures
large-teams

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