We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Data to field name mapping

Development and Programming

mattb's avatar
mattb
2 posts
14 years ago
mattb's avatar mattb

Hi All,

When looking through the EE api / functions etc. I couldn’t seem to find any sort of method which allowed me to pass something an entry_id, and return the data mapped correctly to proper field names. Now, this may not be needed, as it may exist somewhere else within EE that I haven’t yet looked at, but nevertheless; I’ve made it and thought I would share it, in case it comes in handy for anyone else.

public function get_entries_data($entry_id,$join_entries = array()) {
        $this->EE->load->model('Channel_model');
        $channel_data_query = $this->EE->db->query("select ct.title, cd.*, c.field_group from exp_channel_titles ct
                                            left join exp_channel_data cd on (ct.entry_id=cd.entry_id)
                                            left join exp_channels c on (ct.channel_id=c.channel_id)
                                            where ct.entry_id=$entry_id");
        if ($channel_data_query->num_rows()>0) {
            $entry_data = $channel_data_query->result_object();
        } else {
            return false;
        }

        $cur_field_group = null;

        if (is_array($entry_data) && count($entry_data)>0) {
            $i=0;
            foreach($entry_data as $row) {
                $entries[$i] = new stdClass();

                $entries[$i]->title = $row->title;

                if (isset($row->field_group)) {
                    if ($row->field_group != $cur_field_group) {
                        $cur_field_group = $row->field_group;
                        $channel_fields = $this->EE->Channel_model->get_channel_fields($row->field_group)->result_object();
                    }
                } else {
                    return false;
                }

                foreach($row as $column => $value) {
                    //echo $column;
                    foreach($channel_fields as $field) {
                        //echo $column.'-'.$value.'
';
                        if ($column == 'field_id_'.$field->field_id) {
                            //echo $field->m_field_name."\n";
                            $entries[$i]->{$field->field_name} = $value;

                            if (in_array($field->field_name,$join_entries) && isset($value) && !empty($value)) {
                                $entries[$i]->{$field->field_name.'_data'} = $this->get_entries_data($value);
                            }
                        }
                    }
                }
                $i++;
            }
            if (count($entries) == 1) {
                return $entries[0];
            }
            return $entries;
        }
        return false;
    }

You may notice I’ve added something else in there too, which - when the relevant field name is passed - will adjoin the data from another entry, assuming of course the the data in that field is an id which relates to another entry. I know this isn’t strictly correct, and it should probably use the relationships table instead, but it was suitable for my purposes. The fields to join are passed as an array, and the information retrieved will be added to the stdClass as fieldnamepassed_data

If you make any mods - such as the correct use of relationships - it would be good to see them.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.