Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle saving related values if passed as an array of information #4917

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sc0ttkclark
Copy link
Member

@sc0ttkclark sc0ttkclark commented May 7, 2018

Examples below use a "books" pick field, multiple select, related to a "book" custom post type created or extended by Pods).


Example 1

$pod = pods( 'author', 1234 );

$data = array(
    'books' => array(
        array(
            'post_title' => 'Book 1',
            'post_content' => 'Some content',
            'some_custom_field' => 'Value here',
			'post_status' => 'publish',
        ),
    ),
);

$pod->save( $data );

Expectation would be that the above code would save a new value for the related book field.


Example 2

$pod = pods( 'author', 1234 );

$data = array(
    'books' => array(
        array(
            'ID' => 1235,
            'post_title' => 'Book 1',
            'post_content' => 'Some new content',
            'some_custom_field' => 'New value here',
			'post_status' => 'publish',
        ),
    ),
);

$pod->save( $data );

Expectation would be that the above code would update the data for the the related book field.

@sc0ttkclark sc0ttkclark added the Type: Feature Features that add entirely new functionality that was not there before label May 7, 2018
@sc0ttkclark sc0ttkclark added this to the Pods 2.8 milestone May 7, 2018
@sc0ttkclark sc0ttkclark self-assigned this May 7, 2018
@sc0ttkclark sc0ttkclark requested a review from pglewis as a code owner May 7, 2018 04:54
@ghost ghost added the Status: In Progress Issue or PR is currently in progress but not yet done label May 7, 2018
@sc0ttkclark sc0ttkclark changed the base branch from 2.x to release/2.8 July 15, 2020 16:21
@sc0ttkclark sc0ttkclark requested review from JoryHogeveen and removed request for pglewis July 15, 2020 16:21
@sc0ttkclark sc0ttkclark added Status: PR > Pending Code Review PR is pending code review by core developers Status: PR > QA pending QA needs to be done labels Jul 15, 2020
@JoryHogeveen
Copy link
Member

@sc0ttkclark Could you give me a bit more info about the nature of this PR?
What was the bug and how can I replicate that to make sure this PR fixes that?

@sc0ttkclark
Copy link
Member Author

@JoryHogeveen To test this, use the example code. Perhaps we can write tests based on those two?

@pdclark
Copy link
Collaborator

pdclark commented Aug 20, 2021

Tried example 1 without success.
Relationship field is relating "posts" to "posts", since Pods gives an error for "author" that it's a reserved name.
e.g.,

$pod = pods( 'post', 1 ); // The first post; Hello World.

$pod->save(
	[
		// Multi-select relationship field titled "other_posts".
		'other_posts' => [
			[
				'post_title'     => 'Another Post',
				'post_content'   => '',
				'favorite_color' => '#33ee33', // Color Picker field.
			],
		],
	]
);

Expectation was that it would create another post and assign it to the relationship field on post ID 1.
Didn't work for me. Also didn't report any error and ->save() returned (integer) 1.
@sc0ttkclark

@sc0ttkclark sc0ttkclark modified the milestones: Pods 2.8, Pods 2.9 Aug 20, 2021
@sc0ttkclark sc0ttkclark added Status: PR > QA fail QA was done but failed to pass and removed Status: PR > QA pending QA needs to be done labels Aug 20, 2021
@JoryHogeveen
Copy link
Member

JoryHogeveen commented Aug 27, 2021

@sc0ttkclark @pdclark
I can confirm this feature to work properly. It saves the value correctly in the database but in order to display the value properly there is more required.

1: post_status is required since normally the default status is draft and the default requirement for relationships fields is publish
2: post_type is advised since I assume it will otherwise only work with post relationships since that is the default post type.

Working code:

$pod = pods( 'post', 1 ); // The first post; Hello World.

$pod->save(
	[
		// Multi-select relationship field titled "other_posts".
		'other_posts' => [
			[
				'post_status'    => 'publish',
				'post_type'      => 'post',
				'post_title'     => 'Another Post',
				'post_content'   => '',
				'favorite_color' => '#33ee33', // Color Picker field.
			],
		],
	]
);

@pdclark
Copy link
Collaborator

pdclark commented Aug 27, 2021

Great thanks Jory.
For some reason it still didn't work for me.
I tried adding a new post on the init hook:

<?php

add_action( 'init', function(){

	$pod = pods( 'post', 1 ); // The first post; Hello World.

	$pod->save(
		[
			// Multi-select relationship field titled "other_posts".
			'other_posts' => [
				[
					'post_status'    => 'publish',
					'post_type'      => 'post',
					'post_title'     => 'Another Post',
					'post_content'   => '',
					'favorite_color' => '#33ee33', // Color Picker field.
				],
			],
		]
	);

});

Base automatically changed from release/2.8 to main October 18, 2021 17:07
@sc0ttkclark sc0ttkclark modified the milestones: Pods 2.9, Pods 3.0 Jul 25, 2022
@sc0ttkclark sc0ttkclark removed the Status: PR > QA fail QA was done but failed to pass label Jul 25, 2022
@sc0ttkclark sc0ttkclark modified the milestones: Pods 3.0, Backlog Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: In Progress Issue or PR is currently in progress but not yet done Status: PR > Pending Code Review PR is pending code review by core developers Type: Feature Features that add entirely new functionality that was not there before
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants