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

I have a codeigniter website and I want to import Pageviews google analytics chart for each user profile without authnticate button?
facebook login returning 0 codeigniter instead of trying many solutions
Show data from multiple checkbox in codeigniter
How to set up basic auth in rest code-igniter API?
how to add social share buttons on our webpage that share particular page we are recently in codeigniter?
Codeigniter autoload model from inside a folder
want to pass the post value in to controller
jquery file upload send extra data and retrieve it in uploadhandler
Codeigniter pass variable between functions
need to add css,js files inside the modules folder CI hmvc
Why can't I get the csrf token from the hidden input with Codeigniter? [duplicate]
The best temmplate for codeigniter
Is there any way to save cart when a user log out?
Illegal mix of collations for operation 'like' Error Message
Migrations Codeigniter
inputs won't work no apparent reason

Categories

HOME
elastic-beanstalk
typescript
uwp
module
design
wxpython
chronicle
gluu
jqgrid
intel-xdk
visual-studio-2012
geany
restsharp
build.gradle
phpmailer
prestashop-1.7
openframeworks
libssh
textmatebundles
c-strings
ssms-2014
fat-free-framework
desire2learn
trial
kubernetes-go-client
evercookie
chai
webcenter
spring-data-cassandra
xaf
procobol
ase
pseudo-element
uifont
asyncsocket
uservoice
ganglia
tryton
replicate
vb4android
finalcut
mars
jquery-load
subroutine
php-opencloud
ssha
aldryn
many-to-one
spymemcached
protected
translate3d
base-conversion
xerces
bitrock
ash
twill
hebrew
keymapping
cfile
dfsort
data-dictionary
android-lru-cache
appstore-sandbox
resolver
libnids
memoization
argumentexception
speaker
pixelsense
guvnor
hiphop
msxsl
ad-hoc-distribution
ccr
f2c
rails-3.1
apache-config
xquery-update
net-ssh
sqlitemanager
resharper-4.5
graceful-degradation

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