Base_module_model and Base_module_record - shared variables?

edited March 2012 in Feature Requests
Hello!

I'm trying to figure out if it's possible to have a variable inside the model that can be accessed by the record-level object.

just imagine this scenario


class Blocks_model extends Base_module_model {

public $language = 'en_EN';

(...)
}

class Block_model extends Base_module_record {

function get_language()
{
return $this->language
}
}
this obviously fails, because the record object has no way to reach the model (unless the $this->language is a variable of the record object, which is not what i'm looking for)

Is there absolutely no way to achieve this besides having to instantiate the model inside the record ?
ie.


class Block_model extends Base_module_record {

function get_language()
{
$CI = &get_instance();
$CI->load->module_model('fuel', 'blocks_model');
return $CI->blocks_model->language;
}
}
which is a less than optimal solution on many ways... Especially because if you're dealing with the record object -- say, from a controller - that means you've loaded the module already anyway...

Any thoughts?

Comments

  • edited 2:21PM
    There is a reference of the "parent" model passed to the record level object named "_parent_model". Try the following:
    class Block_model extends Base_module_record { function get_language() { return $this->_parent_model->language; } }
  • edited 2:21PM
    Thanks a lot!!! Exactly what i needed!!!
  • Here is the other way around, if you need a record object in the model (to access a certain method or formatted strings).

    $this->map_to_record_class($val) is the key.

    class Locations_model extends Base_module_model {
    
        function list_items($limit = NULL, $offset = NULL, $col = 'name', $order = 'asc', $just_count = FALSE)
        {
            $data = parent::list_items($limit, $offset, $col, $order, $just_count = FALSE);
    
            if (!$just_count)
            {
                foreach($data as $key => $val)
                {
                    // make record object available
                    $valObject = $this->map_to_record_class($val);
                    if($val['latitude'] && $val['longitude']){
                        $static_map = $valObject->get_static_map(['lat' => $val['latitude'],'lng' => $val['longitude']]);
                        $data[$key]['lat_lng'] = sprintf('<img src="%s">', $static_map);
                    }
                }
            }
            return $data;
        }
    }
    
    class Location_model extends Base_module_record {
    
        function get_static_map($lat,$lng)
        {
            return sprintf('http://staticmap.com?lat=%s&lng=%s',$lat,$lng);
        }
    }
    
    
Sign In or Register to comment.