Jump to content

converting a text field to a page select


peterb
 Share

Recommended Posts

here is a snippet that helped me convert a text field to a page field... page fields are the only way currently to have select boxes in the admin.

— I had a text field called client_status on a "client" page template that contained one of 2 values, "good" or "expired"

— created a page in admin named "status".

— then create 2 pages as children of the status page... there titles were "good" "expired"

— next, create a new page field called "client_status_select" and added to the "client" page template

— then make a page template called "update-clients". with the code below.

— lastly, create a page with the "update-clients" template & view it to run the script

$clients = $pages->find("template=client");

    foreach($clients as $c) {

        echo "<li>{$c->url}---{$c->client_status}---{$c->client_status_select}</li>";

$selectpage = $pages->get("template=admin, title=$c->client_status");

        echo "<li>{$selectpage->id}---{$selectpage->title}</li>";

$c->setOutputFormatting(false);

$c->client_status_select =  $selectpage->id;

$c->save();

Link to comment
Share on other sites

I am doing this now, with 5,000 pages & hitting memory limits????

$reports = $pages->find("template=report");

foreach($reports as $r) {

$r->setOutputFormatting(false);

$r->report_status_select =  "12576";

wire('pages')->uncacheAll();

$r->save();

Link to comment
Share on other sites

Peter, looks good but not sure your uncacheAll() is doing anything here since the pages are already cached in $reports (PHP's garbage collector isn't going to unset them as long as there is a reference to them). But if you are running up against a memory limit, try placing a limit on the $reports:

<?php

$start = 0; 
$limit = 500; 

do {
    $reports = $pages->find("template=report, start=$start, limit=$limit"); 
    if(!count($reports)) break;
    foreach($reports as $r) {
        $r->setOutputFormatting(false);
        $r->report_status_select = "123456";
        $r->save(); 
    }
    unset($reports); 
    $pages->uncacheAll();
    $start += ($limit-1);  
} while(1); 

Written in the browser, so code may not be perfect. Let me know if you still run up against any memory limits.

Also in your earlier example:

$c->client_status_select = $selectpage->id;

You can also do just this:

$c->client_status_select = $selectpage; 
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...