Jump to content

Problem adding/populating repeater using API


Francesco Bortolussi
 Share

Recommended Posts

Hello, first thanks for this awesome project.

My question is, I'm importing data from a CSV into pages whose template has a repeater, everything in the page fill's correctly(It even has images attached to it) but no repeater's are added.

Something like page(song) -> repeater(videos of the song)

I copied the code from http://processwire.com/api/fieldtypes/repeaters/ wich is the "Default" or basic way to populate a repeater using API.

require("../../../index.php"); //Add processwire index (Bootstraping)

$template = wire('templates')->get("song");
$parent = wire('pages')->get("/container-songs/");

//Array $rows_from_csv has been previously filled and is correct
//The template has a repeater field called rep_videos

foreach($rows_from_csv as $key=>$song) {  //Start creating each page
    //$esiste = wire('pages')->get("template=canzone,title=$canzone[titolo]");
    $p = new Page();           
    $p->of(false);
    $p->template= $template;
    $p->parent= $parent;     
    $p->title=$key;
    $p->titolo=$song['title'];
    $p->testo=$song['text'];
    $p->description=$song['descrip'];            

    $i++; 
    
    //Load the repeater for each video the song has
    foreach ($song['videos'] AS $v) {               
        if ($v<>'') {
            $i++;                        
           $repeater = $p->rep_videos->getNew();             
           $repeater->title="$key-$i";
           $repeater->video_url = 'http://www.youtube.com/watch?v='.$v;  
           $repeater->save();           
           $p->rep_videos->add($repeater);    
           $p->save(); 
       }              
    }   
}

Maybe the problem is that the code segment is inside a foreach?

I've tryed different approaches, like saving the page before the loop that try to creates the repeater but returns an error:

Error: Uncaught exception WireException with message Can't save page 0: /1444373775-0463-1/: It has no parent

I've tryed saving the page after the foreach so each element of the repeater remain's in memory until finish adding new element's (Video url in this case) with no positive results.

Even tryed to create the repeater of the foreach using literally the code from Repeaters Page

Any help will be appreciated.

Link to comment
Share on other sites

Guys I've got it.
One of the steps I did before starting the creation of pages was delete them.

Apparently:

$delete = wire('pages')->delete($element);

Differ's from:

$delete = wire('pages')->trash($element);

When I deleted the repeater's didn't get filled, but when I trash the element's they did correctly.

Possible Bug?

Why is this happenin'g?

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