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 - downloading file with use of force_download function
I want to retrieve the values from the db using codeigniter
CodeIgniter show errors individually next to form field with ajax callback
Adding rss feed to codeigniter application [closed]
How to unset specific data array in codeigniter
codeigniter ion auth https redirect infinity loop
Expression Engine Module tables and datasort
Session empty codeigniter
Codeigniter Limit not working
codeigniter with boilerplate
Login using email or username in codeigniter
Serving static content from subdomain with codeigniter?
CodeIgniter - Globally load all tables for each controller
Combining Require.js, Backbone.js and a server side MVC framework
Passing adserver tags through Codeigniters global xss filter
how do i change the default behavior of session storing in the database

Categories

HOME
sharepoint-2013
rtmp
blogs
runtime-error
feed
twitter-digits
intel-xdk
delphi-xe7
http2
sqlconnection
session-cookies
frequency
scope
jquery-jtable
group-by
stackexchange-api
nested-set-model
urlrewriter.net
jsgrid
desktop-app-converter
simple-html-dom
meanjs
petrel
machine-language
groove
advanced-installer
msal
perl-module
otroslogviewer
worldpay
stateful
project-centennial
angularjs-routing
simplesamlphp
uservoice
jquery-masonry
libev
playback
iseries-navigator
geneticsharp
lapply
directsound
enhanced-ecommerce
oracle-policy-automation
selenium2library
vb6-migration
uploadify
xlform
balana
hl7-v2
illegalstateexception
dnx
ssha
aldryn
ospf
interpreted-language
tun
hypervisor
wt
oboe.js
structured-programming
sslv3
openxls
janino
horizontalscrollview
mysql5
razor-2
concurrentdictionary
iqueryable
aps
mongo-shell
apache-commons-dbcp
broadcom
datapump
rcu
capitalize
guice-servlet
virtual-earth
slimbox
jmdns
applaud
xml-dtd
cellphone
magento-1.5
ember-router
program-files
ssao
maemo
msn-messenger
web-based

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