passing variable table name to MB_Custom_Table_API::create

Support MB Custom Table passing variable table name to MB_Custom_Table_API::createResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #29356
    Nicholas CoxNicholas Cox
    Participant

    I am trying to create a new database inside my class and I'm passing in '$this->table' to the 'createHousekeepingTable()' function and I can see the string is being passed successfully but the table does not create in the database? I have no errors.

    I replaced the '$this->table' with a string as per the docs and it works fine. For some reason I cannot pass in the variable and make it work.

    Any ideas why?

    
    <?php
    /*
        * Housekeeping Log
        * @since    1.0.0
    */
    
    class Housekeeping
    {
    
        private $wpdb;
        private $table; 
                    
        public function __construct() {
            
            global $wpdb;
            $this->wpdb = $wpdb;
            $this->table = $wpdb->prefix . 'dc_housekeeping';
        }
    
        public function createHousekeepingTable() {
            
            add_action( 'init', 'housekeeping_create_table' );
            function housekeeping_create_table() {
    
                MB_Custom_Table_API::create( "{$this->table}", array(
                    'type' => 'VARCHAR(20) NOT NULL',
                    'date' => 'DATETIME  NOT NULL',
                    'action_date' => 'DATETIME  NOT NULL',
                    'log' => 'VARCHAR(255) NOT NULL'
                ) );
            }
        }
    }
    ?>
    
    #29359
    Long NguyenLong Nguyen
    Moderator

    Hi,

    It is the wrong syntax when creating a function inside a function like that. You can change the code to this one

    class Housekeeping
    {
        private $wpdb;
        private $table; 
                    
        public function __construct() {      
            global $wpdb;
            $this->wpdb = $wpdb;
            $this->table = $wpdb->prefix . 'dc_housekeeping';
            add_action( 'init', array( $this, 'createHousekeepingTable' ) );
        }
    
        public function createHousekeepingTable() {
            MB_Custom_Table_API::create( "{$this->table}", array(
                'type' => 'VARCHAR(20) NOT NULL',
                'date' => 'DATETIME  NOT NULL',
                'action_date' => 'DATETIME  NOT NULL',
                'log' => 'VARCHAR(255) NOT NULL'
            ) );
        }
    }
    
    $housekeeping = new Housekeeping();

    Refer to this topic https://stackoverflow.com/questions/1725165/calling-a-function-within-a-class-method

    #29363
    Nicholas CoxNicholas Cox
    Participant

    Hi Long,

    Ah ok thanks for the detailed response. I totally did not realise I was using the wrong syntax. Thanks for the topic link! problem resolved.

    Nick

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.