• Resolved jaceejoef

    (@jaceejoef)


    Hi,

    I use pandarf_insert function to insert content and it does its job but only if I insert just the name field. I have two fields: name and image.

    This code works:

    $id_int = pandarf_insert(
    	array( 
    		'name' => '11112'
    	), 
    	array( 
    		'child_pod_name' => 'fabric_code', 
    		'parent_pod_id' => 548, // parent cpt id
    		'parent_pod_post_id' => 1201, // the post id
    		'parent_pod_field_id' => 566, // parent cpt field id
    		'user_id' => 1
    	), 
    	true
    );

    This code doesn’t work:

    $id_int = pandarf_insert(
    	array( 
    		'name' => '11112',
    		'image' => '1138' // also tried image link here but does nothing
    	), 
    	array( 
    		'child_pod_name' => 'fabric_code', 
    		'parent_pod_id' => 548, // parent cpt id
    		'parent_pod_post_id' => 1201, // the post id
    		'parent_pod_field_id' => 566, // parent cpt field id
    		'user_id' => 1
    	), 
    	true
    );

    The non-working code returns this SQL:
    INSERT INTO 'wp_osnnwxgsg5_pods_fabric_code' ( 'name','image','pandarf_parent_pod_id','pandarf_parent_post_id','pandarf_pod_field_id','pandarf_created','pandarf_modified','pandarf_modified_author','pandarf_author','pandarf_order' ) VALUES ( 11112,1138,548,1201,566,'2022-03-22 23:18:50','2022-03-22 23:18:50',1,1,3 );
    I execute this SQL and it says “image” is an unknown column.

    To investigate further, I insert the content in the dashboard instead of code and checked how they are placed in the database tables. The content with the name field is added in the wp_osnnwxgsg5_pods_fabric_code table and the image field is added on wp_osnnwxgsg5_podsrel.

    These are the screenshots of the database tables:

    View post on imgur.com

    View post on imgur.com

    • This topic was modified 3 years ago by jaceejoef. Reason: formatting
    • This topic was modified 3 years ago by jaceejoef. Reason: additional info
    • This topic was modified 3 years ago by James Huff.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Coding Panda

    (@codingpanda)

    Hi @jaceejoef

    When I created that function, I was not aware that it didn’t work for files.

    pandarf_insert returns the id when the insert is successful, so the workaround is to use Pods save API: https://docs.pods.io/code/pods/save/

    $id_int = pandarf_insert(
    	array( 
    		'name' => '11112'
    	), 
    	array( 
    		'child_pod_name' => 'fabric_code', 
    		'parent_pod_id' => 548, // parent cpt id
    		'parent_pod_post_id' => 1201, // the post id
    		'parent_pod_field_id' => 566, // parent cpt field id
    		'user_id' => 1
    	), 
    	true
    );
    $pod = pods( 'fabric_code', $id_int );
    $data = array(
            'image' => array(
                'id'    => 1138,   
            )
    );
    $pod->save( $data );

    Let me know if that works.

    Thread Starter jaceejoef

    (@jaceejoef)

    Hi @codingpanda

    Thank you for the workaround code. It works!

    I was able to make it work using an SQL code but your code is neater so I’ll use that.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Insert with additional field’ is closed to new replies.