Jump to content
Sign in to follow this  
fruid

change parent of a page inside a reference field inside a repeater field

Recommended Posts

Inside a Repeater field I have a page reference field. The pages that I select here have a date. I basically want to change the parent (send to the archive) when the date ist older (smaller) than today.
Long story short, I managed to change the parent of the referenced page alright. Also seemingly managed to remove it from the selected items in the page reference field. I can not however save the page for some reason, don't know what I'm doing wrong, the selected pages stay the same in the admin backend.

Here's my code

// this just won't save the changes
$organize = function() use($pages, $page) {
    $reps = $page->reps->find("type=posts_rep"); // the type of repeater items in question
    foreach ($reps as $rep) :
        foreach ($rep->posts as $post) : // loop through the selected referred pages
            $d = date('Ymd', $post->getUnformatted('post_date'));
            $j = date('Ymd');
            if ($d < $j) :
                $page->of(false);
                $rep->posts->remove($post);
                $page->save(); // pass an argument? which one?
                $page->of(true);
            endif;
        endforeach;
    endforeach;
    
    $posts = $pages->find('template=post');
    $today = date('Ymd');
    $archive = $pages->get("id=1075");

// this following part works fine
    foreach ($posts as $post) :
        $post_date = date('Ymd', $post->getUnformatted('post_date'));
        $p = $post->parent;
        $pp = $p->parent;
        if ($post_date < $today && $pp != $archive) :
            $post->of(false);
            $np = $archive->child("title=$p->title");
            $post->parent = $np;
            $post->save();
            $post->of(true);
        endif;
    endforeach;    
};

$organize();

and while I'm at it, I ultimately want to turn this into a cronjob of some sort. Would Lazycron be an option? How would that work, something like: 

$organize->everyDay(86400); // can't be that easy, can it

 

Share this post


Link to post
Share on other sites

[SOLVED] OK so apparently I need to do $rep->save(); instead of $page->save(); which is kind of weird, I thought I had to always save the page where the field is on.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...