Data for Model does not show (except record ID#) plus Warning for line 689

Support MB Custom Table Data for Model does not show (except record ID#) plus Warning for line 689

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #49939
    KGKG
    Participant

    Hello,

    Since the code snippets (below) are a little long, first want to say I appreciate the help!

    Here's the problem I'm having:

    I created a custom model and custom database table, but when I click on the model ("States Model") the editor shows all 63 IDs for the 63 records in the table, but none of the data fields are visible. When I open the database table through LocalWP (AdminNeo), all the data is present for all the records.

    When I click to edit one of the records (or try to add a new record), I receive this Warning:

    Warning: Attempt to read property "post_title" on null in /home/ls/Local Sites/bptest-backup-26-02-17/app/public/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 689

    This is the code from the .php file. Line 689 is the echo statement:

    
    /**
    	 * Output extra data on post forms.
    	 *
    	 * @param WP_Post $post Current post object.
    	 */
    	public function edit_form_top( $post ) {
    		echo '<input type="hidden" id="original_post_title" name="original_post_title" value="' . esc_attr( $post->post_title ) . '" />';
    	}
    

    In case some background information is helpful. The model I'm trying to create is for geolocation/mapping and will only be used for looking up values. Since it's census bureau / tiger web data, it will be checked for accuracy once or twice a year, but that's about it. In other words, I don't need a post for the data, but I would like to see it in the back end for testing purposes, etc.

    Cutting and pasting, messed the spacing up, but here's the code I used to create the model / custom table / metabox:

    
    // Step 1: Register a model.
    add_action( 'init', function() {
    	mb_register_model( 'statesmodel', [
    		'table'  => 'cc_geo_states',
    		'labels' => [
    			'name'          => 'States Model',
    			'singular_name' => 'State',
    			'add_new_item' => 'Add State',
    			'edit_item' => 'Edit State',
    			'search_items' => 'Search States',
    			'not_found' => 'State Not Found',
    			'all_items' => 'States and Territories',
    			'item_updated' => 'State Updated',
    			'item_added' => 'State Added',
    			'item_deleted' => "State Deleted"
    		],
    		'menu_icon' => 'dashicons-location-alt'
    	] );
    } );
    
    add_action( 'init', function () {
    	MetaBox\CustomTable\API::create(
    		'cc_geo_states',                // Custom table name.
    		[                                 // List of columns with data types.
    			'st_name_id'		=> 'INT'
    			'stype'             => 'VARCHAR(1)',
    			'state_base_name'   => 'VARCHAR(45)',
    			'state_abbr'        => 'VARCHAR(2)',
    			'mtfcc'             => 'VARCHAR(5)',
    			'oid'               => 'BIGINT',
    			'geoid'             => 'VARCHAR(12)',
                            'state'             => 'VARCHAR(2)',
                            'statens'           => 'VARCHAR(8)',
                            'region'            => 'VARCHAR(1)',
                            'division'          => 'VARCHAR(1)',
                            'state_full_name'   => 'VARCHAR(45)',
                            'lsadc'             => 'VARCHAR(2)',
                            'funcstat'          => 'VARCHAR(1)',	
                            'arealand'          => 'DOUBLE',	
                            'areawater'         => 'DOUBLE',
                            'centlat'           => 'DECIMAL (10,8)',
                            'centlon'           => 'DECIMAL (11,8)',
                            'intptlat'          => 'DECIMAL (10,8)',
                            'intptlon'          => 'DECIMAL (11,8)',
    		],
    		[ 'state_abbr' ],                      // List of columns that will be indexed.
    	);
    } );
    
    add_filter( 'rwmb_meta_boxes', function ( $metaboxes ) {
        $meta_boxes[] = [
            'title'         => 'States Details',
            'models'        => ['statesmodel'],
            'storage_type'  => 'custom_table',
            'table'         => 'cc_geo_states',
            'fields'        => [
                [
    	        'id'   => 'state_name_id',
                    'name' => 'state_name_id',
    	        'type' => 'number',
    	    ],
                [
                    'id'   => 'stype',
                    'name' => 'S Type',           
                    'type' => 'text',
                ],
            	
    				'id'   => 'state_base_name',
    				'name' => 'state_base_name',
    				'type' => 'text',
                    'admin_columns' => true,
    			],
    			[
    				'id'   => 'state_abbr',
    				'name' => 'state_abbr',
    				'type' => 'text',
                    'admin_columns' => true,               
    			],
    			[
    				'id'   => 'mtfcc',
    				'name' => 'mtfcc',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'oid',
    				'name' => 'oid',
    				'type' => 'number'
    			],
    			[
    				'id'   => 'geoid',
    				'name' => 'geoid',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'state',
    				'name' => 'state',
    				'type' => 'text',
    			],			
    			[
    				'id'   => 'statens',
    				'name' => 'statens',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'region',
    				'name' => 'region',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'division',
    				'name' => 'division',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'state_full_name',
    				'name' => 'state_full_name',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'lsadc',
    				'name' => 'lsadc',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'funcstat',
    				'name' => 'funcstat',
    				'type' => 'text',
    			],
    			[
    				'id'   => 'arealand',
    				'name' => 'arealand',
    				'type' => 'number',
    			],
    			[
    				'id'   => 'areawater',
    				'name' => 'areawater',
    				'type' => 'number',
    			],
    			[
    				'id'   => 'centlat',
    				'name' => 'centlat',
    				'type' => 'number',
                    'admin_columns' => true,
    			],
    			[
    				'id'   => 'centlon',
    				'name' => 'centlon',
    				'type' => 'number',
                    'admin_columns' => true,
    			],
    			[
    				'id'   => 'intptlat',
    				'name' => 'intptlat',
    				'type' => 'number',
                    'admin_columns' => true,
    			],
    			[
    				'id'   => 'intptlon',
    				'name' => 'intptlon',
    				'type' => 'number',
                    'admin_columns' => true,
    				
    			],
            ]
        ];
    
    	return $meta_boxes;
    

    Thanks, Kellie

    #49946
    PeterPeter
    Moderator

    Hello,

    Thanks for reaching out.

    I found 2 issues in your code that register the custom table and custom fields:

    1. The column title is st_name_id but the field ID is state_name_id. The column title must match the field ID, following the documentation https://docs.metabox.io/extensions/mb-custom-table/#notes

    There is an error in the debug log related to this issue
    WordPress database error Unknown column 'state_name_id' in 'field list' for query INSERT INTOcc_geo_states(state_name_id,stype,state_base_name,state_abbr,mtfcc,geoid,state,statens`) VALUES ('23', 'a', 'a', 'a', 'a', 'a', 'a', 'a') made by do_action('load-toplevel_page_model-statesmodel'), WP_Hook->do_action, WP_Hook->apply_filters, MetaBox\CustomTable\Model\Admin->load_add_edit, do_action('mbct_model_edit_load'), WP_Hook->do_action, WP_Hook->apply_filters, MetaBox\CustomTable\Model\MetaBox->save_model, RW_Meta_Box->save_post, do_action('rwmb_after_save_post'), WP_Hook->do_action, WP_Hook->apply_filters, MetaBox\CustomTable\Loader->update_object_data, MetaBox\CustomTable\Storage->insert_row
    `

    2. The parameter passed to the callback function is $metaboxes but inside the callback function, you use a different variable $meta_boxes (underscore).

    After fixing 2 issues, I can create a model entry and edit existing entries properly.

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