Jump to content

How to copy field data from one $page to another.


Neeks
 Share

Recommended Posts

What is the best way to copy all the field data from one page object to another without overwriting things like ID, and name? or anything else that won't change the original pages contextual methods like. parent(), next(), from working as they would normally, 
 

I want to replace my $page object on-load using with another $page of identical field structure just different content.  

Link to comment
Share on other sites

I don't see what this should be good for. If you set the current page to be another instead, then just get the other page.

$page = $pages->get("/someotherpage/");

Doing this at start of output will change current page to the other, so it's indentical and all will render as it would be the other page just having the url of the previous.

Link to comment
Share on other sites

Hi Soma,

Thanks for the reply. I wish it was that easy. I need.  $page->methods(), to work as if they belonged to the original page. But have content from "someotherpage". Maybe that is really easy, maybe I just need to save and restore $originalPage->id and $originalPage->name and all the methods will still work. I'm not sure. Reading the API docs it seems like its not something that a lot of people need to do.
 

The why of all this silliness: my company is investigating PW for a large project. i'm wanting to setup a revision system for multiple developers working out of one central process wire install for content that is pulled down into lots of test environments.  Some times we have to push code and content changes that need to go from testing status to live status at the same time. With lots of devs we are trying not to conflict with each other and overwrite each others changes since we have code branches with content changes that can hang out and diverge from live site content for weeks and need to get integrated at some point. So imagine that situation times many devs. We need to be able to test different versions of content without it effecting live content. We are currently faced with several models. A single centralized server that allows for alternate versions of of pages to be pulled into test environments and loaded if they are available, a locking model which gets us away from merging all together, or to push changes from test environments to our central server using a modified migration tool. The migration tool is great, and i'm playing with it, but we are trying all out options and seeing what is combination of options is going to work best for our workflow. 

The centralized server version works like this:

Dev copies the pages they need to complete their tickets to "/branches/", (automated with a little button called "create branch"). It creates a page relationship between the original page and the copy in the "/branches/" part of the tree. This relation makes it easy and fast to test if a page has an alternate version that we need to load on the front end. (if $page->branchPageFeild == $currentBranchCookie GET "alternate page object") Its pretty important that this page replacement can happen as transparently as possible.  It is also important that all the other functions of the original page work as they normally would as to to not break all the original $page->methods().  if i do a $page = $pages->find('/branch/some-other-page'); my $page->children() calls will be broken unless I copy all the the Original page children, $page->parent() calls would be broken for sure. 

Branching is a necessary evil of our current work flow.  (we do use locking as well, and prefer it to having to merge content, but some times you got to branch and merge content...). Once we get this working on a single server I can play with pushing pages back and forth between servers using json. IE "Push Page to Production Server" Button....

Any thoughts on how to make the first part of this work though ($Page =$NewPage AND Keeps original $Page Methods  )?

Link to comment
Share on other sites

Well you just set the fields from the other page, and the $page will still be the same page object just with new content (on runtime).

$p = $pages->get("/somepage/");
foreach($p->fields as $f){
    $page->$f = $p->$f;
}
  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Well you just set the fields from the other page, and the $page will still be the same page object just with new content (on runtime).

$p = $pages->get("/somepage/");
foreach($p->fields as $f){
    $page->$f = $p->$f;
}

This seems ommit certain fields, eg:

   $fields = array(
      'url',
      'title',
      'description_info',
      'summary',
      'additional_info',
      'hero',
      'images'
    );

    $parent = $pages->get($child->parent_castle->id);

    foreach($fields as $k) {
      if($child->$k) {
        $parent->$k = $child->$k;
      }
    }

    return $parent;

From the code above, only the fields: title, description_info and summary are copied from the source to destination page.

How do I go about copying the url, hero and images fields? The later two are images types obviously.

EDIT:

Setting $child->of(false); and $parent->of(false) allows images to be copied. now only the url will not work.

Link to comment
Share on other sites

Yeah for fields that are objects/arrays you need to turn off outputformatting.

 

I don't really get/see why one would want or have to do this... and wonder if I miss something. If you copy all those fields content to another page including its url at runtime, why not take the page as you have it already?

The url can't be overwritten like this as it is a method and not a property. You would need to hook into Page::path to modify a pages url.

Link to comment
Share on other sites

  • 11 months later...

I don't really get/see why one would want or have to do this... and wonder if I miss something. If you copy all those fields content to another page including its url at runtime, why not take the page as you have it already?

@Soma , your words are starting to sound more and more like wisdom.  at runtime copying all the feilds from one page to another is great. This method falls apart when you try to need to save the page after run time. I found complications with pages with Images....Ckeditor with references to the images. Page Tables, etc.  Had to learn the hard way, but page cloning is really the way to go. 

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...