Last active
December 13, 2018 05:30
-
-
Save damulhan/82c7ca5b0bddd79ef9cde1ec13604e8f to your computer and use it in GitHub Desktop.
Laravel의 Eloquent를 Codeigniter 3 에서 사용하기 (Using Laravel Eloquent in Codeigniter 3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/database": "5.0.28", | |
"illuminate/events": "5.0.28", | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# configs/database.php | |
/* | |
* Start Implementation of Eloquent | |
*/ | |
use Illuminate\Database\Capsule\Manager as Capsule; | |
use Illuminate\Events\Dispatcher; | |
/* -- codeigniters' database setting | |
$active_group = 'default'; | |
$query_builder = TRUE; | |
$db['default'] = array( | |
'dsn' => '', | |
'hostname' => 'localhost', | |
'username' => 'root', | |
'password' => '', | |
'database' => '', | |
'dbdriver' => 'mysqli', | |
'dbprefix' => '', | |
'pconnect' => FALSE, | |
'db_debug' => TRUE, | |
'cache_on' => FALSE, | |
'cachedir' => '', | |
'char_set' => 'utf8', | |
'dbcollat' => 'utf8_general_ci', | |
'swap_pre' => '', | |
'encrypt' => FALSE, | |
'compress' => FALSE, | |
'stricton' => FALSE, | |
'failover' => array(), | |
'save_queries' => TRUE | |
); | |
*/ | |
########################################################################### | |
/* | |
* Create a new capsule | |
*/ | |
$capsule = new Capsule; | |
$capsule->addConnection([ | |
'driver' => 'mysql', | |
'host' => $db['default']['hostname'], | |
'database' => $db['default']['database'], | |
'username' => $db['default']['username'], | |
'password' => $db['default']['password'], | |
'charset' => $db['default']['char_set'], | |
'collation' => $db['default']['dbcollat'], | |
'prefix' => $db['default']['dbprefix'], | |
]); | |
// Make this Capsule instance available globally via static methods... (optional) | |
$capsule->setAsGlobal(); | |
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) | |
$capsule->bootEloquent(); | |
/* | |
* Add Events to CodeIgniter adapted in our own way | |
* LINK: http://jamieonsoftware.com/post/90299647695/using-eloquent-orm-inside-codeigniter-with-added | |
*/ | |
$events = new Dispatcher; | |
$events->listen('illuminate.query', function($query, $bindings, $time, $name) | |
{ | |
// Format binding data for sql insertion | |
foreach ($bindings as $i => $binding) | |
{ | |
if ($binding instanceof \DateTime) | |
{ | |
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); | |
} | |
else if (is_string($binding)) | |
{ | |
$bindings[$i] = "'$binding'"; | |
} | |
} | |
// Insert bindings into query | |
$query = str_replace(array('%', '?'), array('%%', '%s'), $query); | |
$query = vsprintf($query, $bindings); | |
// Add it into CodeIgniter | |
$db =& get_instance()->db; | |
$db->query_times[] = $time; | |
$db->queries[] = $query; | |
}); | |
$capsule->setEventDispatcher($events); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
/* Example code */ | |
class Welcome extends CI_Controller { | |
/** | |
* Index Page for this controller. | |
* | |
* Maps to the following URL | |
* http://example.com/index.php/welcome | |
* - or - | |
* http://example.com/index.php/welcome/index | |
* - or - | |
* Since this controller is set as the default controller in | |
* config/routes.php, it's displayed at http://example.com/ | |
* | |
* So any other public methods not prefixed with an underscore will | |
* map to /index.php/welcome/<method_name> | |
* @see http://codeigniter.com/user_guide/general/urls.html | |
*/ | |
public function __construct() { | |
parent::__construct(); | |
$this->load->model('Person'); | |
$this->load->model('Telephone'); | |
$this->load->model('Telephone_type'); | |
} | |
public function index() | |
{ | |
$persons = Person::all(); | |
foreach ( $persons as $person ) { | |
echo "Name: " . $person->name . '<br><br>'; | |
$telephones = $person->telephone; | |
#print_r($telephones); | |
foreach ( $telephones as $telephone ) { | |
echo $telephone->telephone_type->type . ': ' . | |
$telephone->telephone . '<br>'; | |
} | |
echo '<hr>'; | |
} | |
} | |
/* | |
--------- input() output prints ------- | |
Name: Elvis Presley | |
Home: 12025554444 | |
Mobile: 18029997777 | |
Name: John Green | |
Home: 120233334877 | |
Name: Joseph Hamilton | |
------------------------------- | |
*/ | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## if you want to load eloquent model from global context; | |
## $ci_path = realpath(__DIR__ . 'codeigniter_path') . '/'; | |
## $ci_system_path = $ci_path . 'system/'; | |
## define('BASEPATH', str_replace('\\', '/', $ci_system_path)); | |
function load_model($model) | |
{ | |
global $ci_path; | |
$_ci_model_paths = [ $ci_path . 'application/' ]; | |
$model = ucfirst(strtolower($model)); | |
$path = ''; | |
foreach ($_ci_model_paths as $mod_path) | |
{ | |
if ( ! file_exists($mod_path.'models/'.$path.$model.'.php')) | |
{ | |
continue; | |
} | |
require_once($mod_path.'models/'.$path.$model.'.php'); | |
$_ci_models[] = $name; | |
return new $model(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
### Sample models | |
# models/Telephone.php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use Illuminate\Database\Eloquent\Model as Eloquent; | |
class Telephone extends Eloquent { | |
public $timestamps = true; | |
protected $table = 'test_telephone'; | |
protected $primaryKey = 'id'; | |
protected $fillable = [ | |
'person_id', | |
'telephone_type_id']; | |
function person() { | |
return $this->belongsTo('Person'); | |
} | |
function telephone_type() { | |
return $this->hasOne('Telephone_type', 'id', 'telephone_type_id'); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
<?php | |
# models/Person.php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use Illuminate\Database\Eloquent\Model as Eloquent; | |
class Person extends Eloquent { | |
public $timestamps = false; | |
protected $table = 'test_person'; | |
protected $primaryKey = 'id'; | |
protected $fillable = ['name']; | |
function telephone() { | |
return $this->hasMany('Telephone', 'person_id'); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
<?php | |
# models/Telephone_type.php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use Illuminate\Database\Eloquent\Model as Eloquent; | |
class Telephone_type extends Eloquent { | |
public $timestamps = true; | |
protected $table = 'test_telephone_type'; | |
protected $primaryKey = 'id'; | |
protected $fillable = ['type']; | |
function telephone() { | |
return $this->belongsTo('Telephone'); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE IF NOT EXISTS `test_person` ( | |
`id` bigint(20) NOT NULL AUTO_INCREMENT, | |
`name` varchar(50) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; | |
INSERT INTO `test_person` (`id`, `name`) VALUES | |
(1, 'Elvis Presley'), | |
(2, 'John Green'), | |
(3, 'Joseph Hamilton'); | |
CREATE TABLE IF NOT EXISTS `test_telephone` ( | |
`id` bigint(20) NOT NULL AUTO_INCREMENT, | |
`person_id` bigint(20) NOT NULL, | |
`telephone_type_id` bigint(20) NOT NULL, | |
`telephone` bigint(14) NOT NULL, | |
PRIMARY KEY (`id`), | |
KEY `fk_telephone_person_idx` (`person_id`), | |
KEY `fk_telephone_telephone_type1_idx` (`telephone_type_id`), | |
CONSTRAINT `fk_telephone_person` FOREIGN KEY (`person_id`) REFERENCES `test_person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, | |
CONSTRAINT `fk_telephone_telephone_type1` FOREIGN KEY (`telephone_type_id`) REFERENCES `test_telephone_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; | |
INSERT INTO `test_telephone` (`id`, `person_id`, `telephone_type_id`, `telephone`) VALUES | |
(1, 1, 1, 12025554444), | |
(2, 1, 3, 18029997777), | |
(3, 2, 1, 120233334877); | |
CREATE TABLE IF NOT EXISTS `test_telephone_type` ( | |
`id` bigint(20) NOT NULL AUTO_INCREMENT, | |
`type` varchar(20) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; | |
INSERT INTO `test_telephone_type` (`id`, `type`) VALUES | |
(1, 'Home'), | |
(2, 'Work'), | |
(3, 'Mobile'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment