Support Forum
Support › MB Custom Table › passing variable table name to MB_Custom_Table_API::createResolved
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'
) );
}
}
}
?>
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
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