Support

Home Forums Event Espresso Premium Understanding models and db table creation

Understanding models and db table creation

Posted: December 23, 2022 at 6:34 am


TheFreshUK

December 23, 2022 at 6:34 am

Hi

I’m trying to understand a little more about how EE4 can help with our requirements.

I’ve created a custom model in our add-on that creates a relationship between users. So we’ve created the Model and a DMS.

I’m not entirely sure what I’m missing (as the documentation isn’t quite clear on how to get these working nicely together).

I’ve looked at other add-ons that create their own models and we don’t seem to be doing anything different but can’t seem to get the database to be created.

Our previous solution has been to use the $wpdb global and run raw SQL queries to set up a table and functions to query what we need. However this would be better placed as an EEM so we have better integration with EE4.

DMS


<?php

if (!defined('EVENT_ESPRESSO_VERSION'))
	exit('No direct script access allowed');

/**
 * 
 * EE_DMS_LD_WP_Users_1_0_0
 * 
 * @package    Event Espresso
 * @subpackage eea-ld-wp-users
 * @author     The Fresh UK
 */
class EE_DMS_LD_WP_Users_1_0_0 extends EE_Data_Migration_Script_Base
{
    public function __construct() {
        $this->_pretty_name = esc_html__("Data Migration to LD WP Users 1.0", "event_espresso");
        $this->_migration_stages = array(
        );
        parent::__construct();
    }

    public function can_migrate_from_version($current_database_state_of)
    {
        return FALSE;
    }

    public function schema_changes_after_migration()
    {
        
    }

    public function schema_changes_before_migration()
    {
        global $wpdb;

        $this->_table_is_new_in_this_version('esp_londec_users', "
            BG_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            BU_ID bigint(20) unsigned DEFAULT NULL,
            DU_ID bigint(20) unsigned DEFAULT NULL,
            REG_ID bigint(20) unsigned DEFAULT NULL,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY ld_billing_id_fk (BU_ID),
            KEY ld_delegate_id_fk (DU_ID),
            KEY ld_registration_id_fk (REG_ID),
            CONSTRAINT ld_billing_id_fk FOREIGN KEY (BU_ID) REFERENCES {$wpdb->prefix}users(ID) ON DELETE CASCADE,
            CONSTRAINT ld_delegate_id_fk FOREIGN KEY (DU_ID) REFERENCES {$wpdb->prefix}users(ID) ON DELETE CASCADE,
            CONSTRAINT ld_registration_id_fk FOREIGN KEY (REG_ID) REFERENCES {$wpdb->prefix}esp_registration(ID) ON DELETE CASCADE"
        );
    }
}

Model


<?php
class EEM_LD_User extends EEM_Base
{
    /**
     * private instance of the EEM_LD_User object
     * 
     * @type EEM_LD_User
     */
    protected static $_instance = null;

    /** 
     * Constructor
     * 
     * @param null $timezone
     * @param ModelFieldFactory $model_field_factory
     * @throws EE_Error
     * @throws InvalidArgumentException
     */
    protected function __construct($timezone, ModelFieldFactory $model_field_factory)
    {
        $this->singular_item = esc_html__('Billing Group', 'event_espresso');
        $this->plural_item = esc_html__('Billing Groups', 'event_espresso');

        global $wpdb;

        $this->_tables = array(
            'Billing_Group' => new EE_Primary_Table('esp_ld_users', 'BG_ID'),
        );
        $this->_fields = array(
            'Billing_Group' => array(
                'BG_ID' => new EE_Primary_Key_Int_Field('BG_ID', esc_html__("Billing Group ID", 'event_espresso')),
                'BU_ID' => new EE_Foreign_Key_Int_Field('BU_ID', __('Billing User ID', 'event_espresso'), false, 0, 'WP_User'),
                'DU_ID' => new EE_Foreign_Key_Int_Field('DU_ID', __('Delegate User ID', 'event_espresso'), false, 0, 'WP_User'),
                'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', __('Registration ID', 'event_espresso'), false, 0, 'Registration'),
            )
        );
        $this->_model_relations = array(
            'WP_User' => new EE_Belongs_To_Relation(),
            'Registration' => new EE_Belongs_To_Relation()
        );
        parent::__construct($timezone);
    }
}

they all live in core/ in database_migration_scripts and db_models accordingly and have been registered in the addon.

What am I missing?


Tony

  • Support Staff

December 29, 2022 at 10:17 am

Hi there,

We would need to see the entire add-on, for example, did you register the dms_paths?

No migration stages?

However, I’m asking these questions but the forums aren’t the best place to get an answer to questions like these.

I recommend opening up an issue on the GitHub repo HERE.

Generally, we don’t provide support for 3rd party code, however, I’m sure one of our developers can help point you in the right direction here especially as this relates to our model usage and documentation ๐Ÿ™‚


TheFreshUK

January 5, 2023 at 9:20 am

Thanks Tony, I’ve created an issue on github.


Tony

  • Support Staff

January 5, 2023 at 3:26 pm

Awesome ๐Ÿ™‚

The support post ‘Understanding models and db table creation’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso