Can't Create A Custom Table for the Custom Model!

Support MB Custom Table Can't Create A Custom Table for the Custom Model!

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #41819
    Ahmed E.Ahmed E.
    Participant

    Hi There,

    I'm creating a custom model following the instructions here.

    Everything is working fine except Step 2: Create a custom table for the model, as the table does not get created in the DB and no data is saved!

    Here's the code I'm using to create the table:

    
    // Step 2: Create a custom table for the model.
    add_action('init', function () {
       MetaBox\CustomTable\API::create(
          'wp_provider_plans',     // Table name.
          [  // Table columns (without ID).
             'provider' => 'BIGINT(20) UNSIGNED [0]',
             'service' => 'VARCHAR(255)',
             'price' => 'FLOAT',
          ],
          ['provider'],               // List of index keys.
          true                        // Must be true for models.
       );
    });
    

    How to solve this problem?

    #41822
    PeterPeter
    Moderator

    Hello,

    Please access the database and try to create a test table manually and see how it goes.

    If it is created, then try to deactivate all plugins except Meta Box, MB extensions and switch to the default theme of WordPress and check the issue again
    If it is not created, then check the permission of the user if you have full privilege to create a table in the database.

    #41829
    Ahmed E.Ahmed E.
    Participant

    Hi Peter,

    I've created the table manually since the feature is not working.

    Currently, there is another problem.

    Is there a filter that runs only when a new record is created (does not work on updates) and has access to form data?

    Basically, I'm looking for a Custom Model filter similar to rwmb_before_save_post that runs only once when a new record is inserted.

    I'm recording the user's IP in the DB, and if the filter runs on updates, the IP of the admin/editor will be updated!

    T.I.A.

    #41836
    PeterPeter
    Moderator

    Hello,

    You can try to use the function \MetaBox\CustomTable\API::exists() in the callback function, which hooks to rwmb_before_save_post action, to check if there is a row in the custom table for the current post (object) and do your stuff then.

    Refer to the documentation https://docs.metabox.io/extensions/mb-custom-table/#exists

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