Foreign Key data not loaded

edited July 2015 in Modules
Hi,
I have a problem accessing the data via a foreign key. I have a model Stages_model
class Stages_model extends Abstract_module_model { public $required = array('name'); public $unique = array('name'); public $foreign_keys = array('stagetype' => 'stage_types_model', 'last_modified_by' => 'fuel_users_model'); public $has_many = array('stage_images' => 'stage_images_model'); public $hidden_fields = array('last_modified'); function __construct() { parent::__construct('fw_stages'); } /** * Lists the module's items * * @access public * @param int The limit value for the list data (optional) * @param int The offset value for the list data (optional) * @param string The field name to order by (optional) * @param string The sorting order (optional) * @param boolean Determines whether the result is just an integer of the number of records or an array of data (optional) * @return mixed If $just_count is true it will return an integer value. Otherwise it will return an array of data (optional) */ public function list_items($limit = NULL, $offset = 0, $col = 'id', $order = 'asc', $just_count = FALSE) { $this->db->join('stage_types', 'stage_types.id = stages.stagetype'); $this->db->join('users', 'users.id = stages.last_modified_by'); $this->db->select('stages.id as id, stages.name as name, stage_types.name as stage_type, img_count, date_added, last_modified, users.user_name as last_modified_by, published'); $data = parent::list_items($limit, $offset, $col, $order, $just_count); return $data; } /** * Add specific changes to the form_fields method * * @access public * @param array Values of the form fields (optional) * @param array An array of related fields. This has been deprecated in favor of using has_many and belongs to relationships (deprecated) * @return array An array to be used with the Form_builder class */ public function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); $options = array('yes' => 'ja', 'no' => 'nein'); $fields['randomize'] = array('label' => lang('form_label_stage_randomize'), 'comment' => lang('form_comment_stage_randomize'), 'type' => 'enum', 'options' => $options ); $fields['stagetype'] = array('label' => lang('form_label_stage_type')); $fields['img_count'] = array('label' => lang('form_label_stage_image_count'), 'comment' => lang('form_comment_stage_image_count'), 'ignore_represantative' => true, 'type' => 'int'); $fields['last_modified']['label'] = lang('form_label_last_modified'); $fields['last_modified_by']['label'] = lang('form_label_last_modified_by'); $fields['stage_images']['label'] = lang('form_label_stage_images'); return $fields; } } class Stage_model extends Abstract_module_record { public function save($validate = TRUE, $ignore_on_insert = TRUE, $clear_related = NULL) { $this->last_modified = datetime_now(); $saved = parent::save($validate = TRUE, $ignore_on_insert = TRUE, $clear_related = NULL); return $saved; } }
and the corresponding foreign key model
class Stage_Types_model extends Abstract_module_model { function __construct() { parent::__construct('fw_stage_types'); } /** * Add specific changes to the form_fields method * * @access public * @param array Values of the form fields (optional) * @param array An array of related fields. This has been deprecated in favor of using has_many and belongs to relationships (deprecated) * @return array An array to be used with the Form_builder class */ public function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); $options = array('pictures', 'pictures small'); $fields['css_outer_class'] = array('label' => lang('form_label_stagetype_css_outer_class'), 'type' => 'select', 'options' => $options); $options = array('slidewrapper', 'slidewrapper smallstage'); $fields['css_slidewrapper_class'] = array('label' => lang('form_label_stagetype_css_slidewrapper_class'), 'type' => 'select', 'options' => $options); return $fields; } }

Now in my main layout I want to access the data.
$stage = fuel_model('stages', array('find' => 'one', 'where' => array('id' => $stage_id)));

According to the userguide I should access the foreign key data via $stage->stage_type but this attribute is nULL.
what is the correct way?

Comments

  • edited 7:00AM
    I believe it's $stage->stagetype without the hyphen correct?
  • edited 7:00AM
    that is the name of the foreign key attribute in the database.
    Using this in the code results in the value 1 (the id of the entry in the database) beeing returned. I expected to retrieve the corresponding object.
  • edited 7:00AM
    I believe if you change the field name to stagetype_id, then stagetype will be the object.
  • edited 7:00AM
    ah ok. renamed the field to type_id and then I can access it via ->type
    Thanks!!!!
Sign In or Register to comment.