Skip to content

Instantly share code, notes, and snippets.

@damulhan
Last active December 13, 2018 05:30
Show Gist options
  • Save damulhan/82c7ca5b0bddd79ef9cde1ec13604e8f to your computer and use it in GitHub Desktop.
Save damulhan/82c7ca5b0bddd79ef9cde1ec13604e8f to your computer and use it in GitHub Desktop.

Revisions

  1. damulhan revised this gist Dec 13, 2018. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -43,5 +43,17 @@ public function index()
    }
    }

    /*
    --------- input() output prints -------
    Name: Elvis Presley
    Home: 12025554444
    Mobile: 18029997777
    Name: John Green
    Home: 120233334877
    Name: Joseph Hamilton
    -------------------------------
    */
    }

  2. damulhan revised this gist Dec 13, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ public function index()
    foreach ( $persons as $person ) {
    echo "Name: " . $person->name . '<br><br>';
    $telephones = $person->telephone;
    print_r($telephones);
    #print_r($telephones);
    foreach ( $telephones as $telephone ) {
    echo $telephone->telephone_type->type . ': ' .
    $telephone->telephone . '<br>';
  3. damulhan revised this gist Dec 13, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    /* Example code */
    class Welcome extends CI_Controller {

    /**
    @@ -24,8 +25,7 @@ public function __construct() {
    $this->load->model('Telephone');
    $this->load->model('Telephone_type');
    }



    public function index()
    {

  4. damulhan revised this gist Dec 13, 2018. No changes.
  5. damulhan revised this gist Dec 13, 2018. 2 changed files with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions configs-->database.php
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    use Illuminate\Database\Capsule\Manager as Capsule;
    use Illuminate\Events\Dispatcher;

    /* -- codeigniters' database setting
    $active_group = 'default';
    $query_builder = TRUE;

    @@ -31,6 +32,7 @@
    'failover' => array(),
    'save_queries' => TRUE
    );
    */

    ###########################################################################

    File renamed without changes.
  6. damulhan revised this gist Oct 26, 2016. 2 changed files with 4 additions and 33 deletions.
    32 changes: 0 additions & 32 deletions controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,6 @@
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');



    #############
    # Eloquent loader

    ## $ci_path = realpath(__DIR__) . '/../../';
    ## $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();
    }
    }
    #############



    class Welcome extends CI_Controller {

    /**
    5 changes: 4 additions & 1 deletion eloquent_helper.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    ## $ci_path = realpath(__DIR__ . '/../../modules/ci/') . '/';

    ## 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));

  7. damulhan revised this gist Oct 26, 2016. 3 changed files with 55 additions and 27 deletions.
    27 changes: 0 additions & 27 deletions configs-->database.php
    Original file line number Diff line number Diff line change
    @@ -90,30 +90,3 @@

    $capsule->setEventDispatcher($events);



    #############

    ## $ci_path = realpath(__DIR__ . '/../../modules/ci/') . '/';
    ## $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();
    }
    }
    32 changes: 32 additions & 0 deletions controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,38 @@
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');



    #############
    # Eloquent loader

    ## $ci_path = realpath(__DIR__) . '/../../';
    ## $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();
    }
    }
    #############



    class Welcome extends CI_Controller {

    /**
    23 changes: 23 additions & 0 deletions eloquent_helper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ## $ci_path = realpath(__DIR__ . '/../../modules/ci/') . '/';
    ## $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();
    }
    }
  8. damulhan revised this gist Oct 26, 2016. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions configs-->database.php
    Original file line number Diff line number Diff line change
    @@ -89,3 +89,31 @@
    });

    $capsule->setEventDispatcher($events);



    #############

    ## $ci_path = realpath(__DIR__ . '/../../modules/ci/') . '/';
    ## $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();
    }
    }
  9. damulhan revised this gist Oct 26, 2016. 1 changed file with 0 additions and 22 deletions.
    22 changes: 0 additions & 22 deletions models-->Telephone_type.php
    Original file line number Diff line number Diff line change
    @@ -1,22 +0,0 @@
    <?php

    ### Sample models

    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');
    }
    }



  10. damulhan renamed this gist Oct 26, 2016. 1 changed file with 26 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    ### Sample models

    # models/Telephone.php

    defined('BASEPATH') OR exit('No direct script access allowed');

    use Illuminate\Database\Eloquent\Model as Eloquent;
    @@ -25,10 +27,12 @@ function telephone_type() {
    }


    /////////////////
    ////////////////////////////////////////////////////////////////////////////

    <?php

    # models/Person.php

    defined('BASEPATH') OR exit('No direct script access allowed');

    use Illuminate\Database\Eloquent\Model as Eloquent;
    @@ -45,5 +49,26 @@ function telephone() {
    }
    }

    ////////////////////////////////////////////////////////////////////////////

    <?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');
    }
    }


  11. damulhan revised this gist Oct 26, 2016. 1 changed file with 39 additions and 0 deletions.
    39 changes: 39 additions & 0 deletions test_person_*.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@

    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');
  12. damulhan revised this gist Oct 22, 2016. 2 changed files with 69 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions controller-->Welcome.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    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>';
    }
    }

    }

    22 changes: 22 additions & 0 deletions models-->Telephone.php
    Original file line number Diff line number Diff line change
    @@ -25,3 +25,25 @@ function telephone_type() {
    }


    /////////////////

    <?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');
    }
    }



  13. damulhan created this gist Oct 22, 2016.
    6 changes: 6 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    {
    "require": {
    "illuminate/database": "5.0.28",
    "illuminate/events": "5.0.28",
    }
    }
    91 changes: 91 additions & 0 deletions configs-->database.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@

    # configs/database.php

    /*
    * Start Implementation of Eloquent
    */
    use Illuminate\Database\Capsule\Manager as Capsule;
    use Illuminate\Events\Dispatcher;

    $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);
    27 changes: 27 additions & 0 deletions models-->Telephone.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php

    ### Sample models

    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');
    }
    }


    22 changes: 22 additions & 0 deletions models-->Telephone_type.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    ### Sample models

    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');
    }
    }