Using DataMapper ORM for php to match field names

I am considering using ORM DataMapper with CodeIgniter, but have a scenario in which the database structure is poorly formed.

I would like to be able to configure my model to match database field names, something more logical. Then, when we move on to updating the database structure, I can just update the model and all the reference code will continue to work.

Is this possible with ORM DataMapper?

+3
source share
2 answers

Yes it is possible. I can explain the logic and how you can solve this problem using the special main class for the codeigniter application.

/core/ Mapdm.php

.

class Mapdm {


    private $map_fields = array();


    function __construct($modelClass_name, $map_fields = array()){

        $this->_model = new $modelClass_name(); /* instantiate Model */
        $this->map_fields = $map_fields;

    }

    function __get($name){
        if(isset($this->map_fields[$name])){
            $name = $this->map_fields[$name];
        }

        return $this->_model->{$name};

    }

    function __set($name, $value){

        if(isset($this->map_fields[$name])){
            $name = $this->map_fields[$name];
        }

        return $this->_model->{$name} = $value;

    }

    function __call($name, $args){

        return call_user_func_array(array($this->_model, $method), $args);
    }


}

, , , . , .

. map_field_config.php( )

$config['mapfield'][{modelname}] = array ('fieldA'=>'real_fieldname', 'fieldB'=>'real_fieldname2');

class ControllerName extends CI_Controller{

    function __construct(){

       parent::__construct();

       $mapfields = $this->config->item('mapfield', $mapfields);
       $model_fields = $mapfields['mymodel'];

       $this->mymodel = new Mapdm('mymodel');

    }

    function index(){

      $records = $this->mymodel->get();


       }

 }

, , . - Datamapper . datamapper.

+1

, , ORM. Propel, , , , .

0

All Articles