1

I'm creating a custom member field programmatically. This works fine, except that I want the database column to be dynamic based on what's already in the database.

In my code below, the uniqueFieldId variable could be 2, or 5, or even 999. This should be based on the columns that are already in the database. When m_field_id_2 and m_field_id_3 exist in the database, uniqueFieldId should be 4 so that the function below adds the column m_field_id_4 to the database.

How can I do this?

private function create_custom_member_field()
{
    //ToDo: Find a way to calculate the columns, not the rows.
    $uniqueFieldId = ...;


    // Add new columns in the member_data table
    ee()->dbforge->add_column('member_data', array('m_field_id_' . $uniqueFieldId => array('type' => 'text')));
    ee()->dbforge->add_column('member_data', array('m_field_ft_' . $uniqueFieldId => array('type' => 'tinytext')));


    // Add the actual credits field
    ee()->db->insert('member_fields', array('m_field_id'    => $uniqueFieldId,
                                            'm_field_name'  => 'credits',
                                            'm_field_label' => 'Credits',
                                            'm_field_type'  => 'text'
                                            ));
}

1 Answer 1

0

Sorry to say this, But you are doing it wrong. You first need to add entry in member_fields table and then member_data table.

Like this: (I didn't tested the code)

// Add the actual credits field
$data = array(
    // 'm_field_id'    => $uniqueFieldId, (No need to pass this)
    'm_field_name'  => 'credits',
    'm_field_label' => 'Credits',
    'm_field_type'  => 'text'
);
ee()->db->insert('member_fields', $data);

/*Get ID of column to be set*/
ee()->db->select('m_field_id');
ee()->db->from('member_fields');
ee()->db->where('m_field_name', 'credits');
$id = ee()->db->get()->row('m_field_id');

// Add new columns in the member_data table
ee()->dbforge->add_column('member_data', array('m_field_id_' . $id => array('type' => 'text')));
ee()->dbforge->add_column('member_data', array('m_field_ft_' . $id => array('type' => 'tinytext')));
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.