passing variable table name to MB_Custom_Table_API::create
Support › MB Custom Table › passing variable table name to MB_Custom_Table_API::createResolved
- This topic has 2 replies, 2 voices, and was last updated 5 years ago by
Nicholas Cox.
-
AuthorPosts
-
July 7, 2021 at 10:43 PM #29356
Nicholas Cox
ParticipantI 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' ) ); } } } ?>July 8, 2021 at 12:29 PM #29359Long Nguyen
ModeratorHi,
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
July 8, 2021 at 3:43 PM #29363Nicholas Cox
ParticipantHi 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
-
AuthorPosts
- You must be logged in to reply to this topic.