Jump to content

Migrating fields


statestreet
 Share

Recommended Posts

I have a checkbox field that I need to split into two checkboxes. 

Currently, the single checkbox determines whether a badge appears next to an entry on the front end, but now that badge should only appear if both checkboxes are checked. So when I add the second checkbox, I'll need all the old pages that were created when there was only one checkbox to now have both checkboxes checked so that the badge appears based on the new template logic. 

Any suggestions that would be more efficient than manually going through and editing all the old pages by hand? :)

Link to comment
Share on other sites

You could write a bunch of PHP using the ProcessWire-API into a template file and then just call it once. Or you could do it on the database with one line of SQL. ProcessWire’s tables are very clean and easy to understand. If you have access to PhpMyAdmin, I’d recommend doing it that way. It will be something like

insert into field_mynewcheckbox (page_id, value)
select page_id, value from field_myoldcheckbox

(Treat this as pseudo code)

edit: I should mention this basically clones all values of this field, which is okay in this case, since a checkbox only has 2 states. This is also assuming that you’re using the two checkboxes on all the same templates etc. Otherwise you may want to filter the Select a bit, or just use Craig’s code below.

Link to comment
Share on other sites

This should be pretty easy to do using some code that calls the API :)

<?php

$entries = wire('pages')->find("template=entry, old_check=1");

foreach ($entries as $entry) {

	$entry->of(false);
	echo "Checking new checkbox on {$entry->title}...<br>";
	$entry->new_check = 1;
	$entry->save();

}

The logic is: find all pages that have the old checkbox checked, check the new one, and save the changes.

You could put that in a template file, run it once by visiting a page using that template, and then removing the code. Just remember to change the values for template and checkbox fields to reflect the actual ones you use on your site :)

Edited by Craig A Rodway
  • Like 2
Link to comment
Share on other sites

Craig, you make it look so easy! :)

I think I almost have it, but I neglected to mention my checkbox is in a repeater. This seems to work (the echoes spit out what I expect) but the save doesn't seem to take: 

<?php

$entries = wire('pages')->find("template=story");

foreach ($entries as $entry) {
	$entry->of(false);
	echo "<br>Checking checkbox on {$entry->title}...<br>";
	foreach ($entry->tests as $test) {
		if ($test->old_check == 1) {
			$test->new_check = 1;
			echo "Converted {$test->scenario}<br>";
		}		
	}	
	$entry->save();
}
Link to comment
Share on other sites

Ah, yes, that changes things :)

I haven't really used repeaters, but I think this might work - just treat each repeater item like a page:

<?php

$entries = wire('pages')->find("template=story");

foreach ($entries as $entry) {
	echo "<br>Checking checkbox on {$entry->title}...<br>";
	foreach ($entry->tests as $test) {
		if ($test->old_check == 1) {
			$test->of(false);
			$test->new_check = 1;
			$test->save();
			echo "Converted {$test->scenario}<br>";
		}		
	}	
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...