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

how to use multiple or_where on same field in codeigniter
CodeIgniter - session record not deleted from database (CI - 2.1.4)
Is CodeIgniter 3 Complete?
Call view from view on button click
Ask, how to use redirect without contition?
PHP CodeIgniter and Doctrine
Connecting to SQL Server via CodeIgniter
I get errors using codeigniter email library
Using FontAwesome with CodeIgniter
Error UTF8 - PYROCMS when setting SetEnv PYRO_ENV production in .htaccess
CodeIgniter Validation rules are not working
Convert html to pdf in codeigniter
$(window).on('beforeunload', function() not working on sometimes
Codeigniter query within foreach loop
Check if db->update successful with Codeigniter when potentially no rows are updated
CodeIgniter sess_destroy does not delete user_data session?

Categories

HOME
performance-testing
elf
azure-search
apache-pig
wikipedia
silverlight
bluej
openmodelica
nservicebus
facebook-opengraph
jgit
syswow64
helpers
amplitude
gas
django-autocomplete-light
netcdf
android-button
graphql-dotnet
webfocus
python-behave
toastr
detach
aem-6
vtable
essbase
subscription
linear
integer-programming
docker-swarm-mode
ssms-2012
uifont
xbrl
xssf
derbyjs
attributerouting
jawr
lapply
sizing
informatica-cloud
hard-drive-failure
tfs2008
mink
post-increment
android-popupwindow
asp.net-webpages
tag-helpers
polygons
telescope
base-conversion
fastparse
newsql
lossless-compression
janino
android-tablelayout
catalina.out
before-save
bson
sonata-media-bundle
lightstreamer
android-c2dm
cfile
freemind
onejar
couchrest
siblings
semantic-markup
nsmanagedobjectmodel
datadesign
dynamic-usercontrols
jsr223
azure-role-environment
gemstone
android-3.1
jboss-cache
keylogger
iphone-sdk-3.1

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