Jump to content

Vinnie

Members
  • Posts

    11
  • Joined

  • Last visited

Everything posted by Vinnie

  1. hi @Robin S , You are right, the is_null() will always returns false, and i dont need to remove the times seperately, i edited my code to your last example and it still works fine, thank you for your input.
  2. Hi @BitPoet, thank you for your reply, unfortunately that does not work, I still get the same results. The only way i have made it work so far is to split the code into 2 files/requests, be it via manually executing the 2 files one after the other, or by doing a curl/exec request and executing the second file in the first one.
  3. @flydev @horst To confirm my suspicions i seperated to delete snippet into a different file, and when i run that script first, and then the script to add the data it always works with no errors, so why cant this happen in a single request?
  4. Hi, @flydev i already tried that, i get the same results, also if i just call $p->save() every time. the weird thing is still that it works on fresh database, so the removing code must have something to do with it
  5. Hi @horst, thank for your reply. It's only 2 repeaters ? , 1 for dates, and inside 1 for times. It's for a movie theater so they can set viewing dates/times for different movies. So to clarify: movie page -> date-repeater -> time-repeater. But the strange thing is that on a fresh db, everything works as it should. It is only when the script is run for the second time, and the data needs to be deleted first, that i get the no parent error (if (!is_null($p->film_days)) . The error happens on the line with: $repeaterTime->save(); However when i place a die() after the code that removes the items, all data is deleted from the database, en when editing the page, the repeaters are empty. When i then run the script normally again, no errors are thrown. So it seems to only generate errors when both the deleting of items, and the adding of items, happens in the same request.
  6. Hi everyone! I'm using the api to remove my old repeater items and add new ones. It uses a reapeater where one of the fields is also a repeater, a repeater thats holds dates, and in the dates a repeater that holds times. For this im using the following code: // foreach ($movieInfo as $movie) { $p = $this->pages->get("template=film,title=" . $this->sanitizer->selectorValue($movieInfo[1]->title)); $p->of(false); //create a new page if it doesn't exist yet if (!$p->id) { $p = new ProcessWire\Page(); $p->template = 'film'; $p->parent = $this->pages->get('template=films'); $p->title = $movieInfo[1]->title; echo "created page: " . $p->title . "<br>"; $p->save(); } //remove all the old data if (!is_null($p->film_days)) { if (!is_null($p->film_days->film_times)) { $p->film_days->film_times->removeAll(); $p->save(); } $p->film_days->removeAll(); $p->save(); } // die; //add the new data if (!is_null($movieInfo[1]->shows)) { foreach ($movieInfo[1]->shows as $date => $show) { $repeaterDay = $p->film_days->getNew(); $repeaterDay->film_day = $date; $repeaterDay->save(); foreach ($movieInfo[1]->shows[$date] as $time) { $repeaterTime = $repeaterDay->film_times->getNew(); $repeaterTime->film_time = $time->startTime; $repeaterTime->save(); } } } echo "edited page: " . $p->title . "<br>"; $p->save(); // } This works fine on a fresh database, my movies have several dates with several times added, but when the script is run again, (and the remove code comes into action) I get the following error: Can’t save page 0: /1543400391-5324-2/: It has no parent assigned. The thing i dont understand i when u uncomment that die in the middle, run the code, all items are deleted correctly, when i then comment it and run the script agian it runs without errors. Does anyone know why this cant be run in 1 go? I suspect something with cache or temporary pages the repeater adds, but cant seem t find anything Hope you guys can help! Vincent van Wijk
  7. I have found the problem: when adding the options in my textarea i need to include the id so instead of: cat1 cat2 cat3 I need to fill in 1=cat1 2=cat2 3=cat3 my final code: $this->addHookAfter('Pages::saved', function($event) { $page = $event->arguments[0]; //set the page //get the subcategories from the parent textarea, split on newline $subcats = preg_split('/[\n\r]+/', $page->subcats); //implode the items with a newline $subcats = implode("\n",$subcats); switch ($page->template) { case 'winkels-overzicht': //set the options $field = $this->fields->winkel_categorie; $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true); break; case 'sport-verenigingen-overzicht': //set the options $field = $this->fields->sport_categorie; $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true); break; case 'cultuur-overzicht': //set the options $field = $this->fields->cultuur_categorie; $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true); break; case 'vrijetijd-overzicht': //set the options $field = $this->fields->vrije_tijd_categorie; $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true); break; case 'horeca-overzicht': //set the options $field = $this->fields->horeca_categorie; $this->modules->get('FieldtypeOptions')->manager->setOptionsString($field, $subcats, true); break; default: break; } });
  8. Hi again, Yes this fixed it! I Didnt know I had to use $this in admin.php. I'm just having one little issue now, when i remove a line from the parent textarea, and add something else, iget the following error: Integrity constraint violation: 1062 Duplicate entry 'blabla-134' for key 'title' How can i prevent this? Thanks in advance!
  9. hi Flydev! Thanks a lot for the quick reply. I have read those topics but somehow i still cant get it to work the line $field = $fields->get("sport_categorie"); produces the error: Call to a member function get() on null. Maybe this is because im adding the hook in admin.php? On the other hand admin is just a template so it should have acces to $fields? Thanks in advance!
  10. Hi all, Im trying to fill an options field in all the children of the parent page, after I save it. the options field is configured as multiple select checkboxes. here's the code i have so far: $this->addHookAfter('Pages::saved', function($event) { $page = $event->arguments[0]; //set the page if($page->template == 'sport-verenigingen-overzicht') { //get the subcategories from the parent textarea, split on newline $subcats = preg_split('/[\n\r]+/', $page->subcats); //(also tried without imploding and adding the array, also doesnt work) $subcats = implode("|",$subcats); //get the children $children = $page->children(); foreach ($children as $child) { //set the options(sport_categorie is the options field) $child->sport_categorie = $subcats; $child->save('sport_categorie'); } //if i use a normal textfield instead of an optionsfield, //all the children have the correct data e.g: test1|test2|test3 //how to get the values into the options field?? } }); Hope you guys can help! Keep up the good work, I'm loving what you're doing with PW!!
×
×
  • Create New...