ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Better SQL for weblog field and custom member field column names, names not ids?

March 16, 2009 4:07pm

Subscribe [2]
  • #1 / Mar 16, 2009 4:07pm

    BridgingUnit

    214 posts

    Does anyone have a less cumbersome way of dealing with the strange way EE deals with column naming for data tables?

    At the moment I have this to get specific weblog data:

    //get weblog field details
    $query = $DB->query("SELECT field_id, field_name FROM exp_weblog_fields");     
       
    if ($query->num_rows > 0){
        foreach ($query->result as $row){                
            //have to build our own column names due to EE's funny system
            if($row['field_name'] == 'my_weblog_field_name'){                    
                $col_my_weblog_field_name = 'field_id_'.$row['field_id'];
            }                
        }            
        //get required weblog data
        $query = $DB->query("SELECT ".$col_my_weblog_field_name." AS my_weblog_field_name
            FROM exp_weblog_data wd, exp_weblogs w 
            WHERE w.weblog_id = wd.weblog_id 
            AND wd.entry_id = '".$my_weblog_id."' 
            LIMIT 1");
        
        if ($query->num_rows > 0){
            //success!
        }else{
            //failure
        }
    }else{
        //failure
    }

    And this to get a custom member field:

    //get members custom field details
    $query = $DB->query("SELECT m_field_id, m_field_name FROM exp_member_fields");     
       
    if ($query->num_rows > 0){
        foreach ($query->result as $row){                
            if($row['m_field_name'] == 'my_custom_field'){
                //have to build our own column name due to EE's funny system
                $col_my_custom_field = 'm_field_id_'.$row['m_field_id'];
            }                
        }            
        //get required member data
        $query = $DB->query("SELECT ".$col_my_custom_field." 
            AS my_custom_field 
            FROM exp_member_data 
            WHERE member_id ='".$member_id."' 
            LIMIT 1");
        
        if ($query->num_rows > 0){
            //success
        }else{
            //failure
        }
    }else{
        //failure
    }

    I’d really prefer a single query in both of these instances rather than having to hit the database twice, but can’t come up with a better way of dealing with the fact that column names for weblog_data have names like field_id_18 and for custom members, similarly, fields like m_field_id_13.

    Any tips gratefully received. Thank you.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases