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-Checking session variable is already set or not when clicking each link or reloading page
Where fieldname is null using active record
how to send array of values to a query dynamically in codeigniter
Codeignitor active record nested select issue
Codeigniter and i18n for ajax url
How to debug CI-Merchant Paypal Pro integration?
Different user levels
codeigniter session is not storing all data
Dynamic Get Clumn value
Pagination CodeIgniter: Page, wrong link
codigniter limit function not working with $this->db->query($sql) [closed]
add_column bug for Postgres with Codeigniter?
HybridAuth keeps giving “User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.”
can't navigate first uri segment (controller) in wamp
pyrocms Unable to connect to your database server using the provided settings. Filename: /libraries/MX/Loader.php Line Number: 104
How to output each data from session to view

Categories

HOME
typescript
wpf
matlab
odoo-10
music
gwt
runnable
watson
outlook
typeahead.js
normalization
sony-camera-api
build.gradle
tin-can-api
plotly
h2o
zebble
wavelet
eloqua
facebook-opengraph
calabash-android
rebus
imp
qualtrics
afnetworking-2
atmelstudio
formio
gyroscope
freetype
revitpythonshell
accelerated-mobile-page
opencms
spark-graphx
django-autocomplete-light
precision
apache-httpcomponents
regex-group
text-parsing
android-button
apscheduler
expression-trees
scatter-plot
react-redux-form
jtapi
background-process
inria-spoon
fipy
outlook-vba
argparse
partition
mindstorms
mamp-pro
php-5.5
tabview
move
ewsjavaapi
uservoice
domdocument
simple.odata.client
infiniband
fasta
flying-saucer
glumpy
jcuda
mailcore2
flattr
building
xnamespace
python-3.1
uploadify
isapi-rewrite
mars
okuma
asp.net-webpages
storage-duration
occam-pi
java-gstreamer
uac
polygons
telescope
treegrid
maven-cargo
centering
vendor
servlet-listeners
esx
pkcs#10
short
master-theorem
pdf-scraping
android-lru-cache
simpleaudioengine
emitmapper
glulookat
memoization
rcu
progressdialog
src
memento
dependency-walker
sender
oncreate
rc-shell
ccr
eye-detection
scramble
msn-messenger
eclipse-tptp
palm-pre

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