herr rilke Posted August 19 Share Posted August 19 hi everyone, after importing 1500 pages from CSV I want to set the value of a page reference field. the recordsets hav a field called "sammelband" containing a string of the title i am looking for. what i want to achive is to find a page with that name and to save the page as a page reference. because the it is possible to link to that page later on in the output. both fields are in the same template "publikation". the pages are set hidden. the problem seems to occur in the saving part of the script. all paramenters work like expected - except that the page / value is not stored in the database. i'm using processwire 3.0.240 and the following code in the parentpage: foreach ($page->children("include=hidden") as $child) { if (! empty($child->sammelband_text)){ // doing some sanitation to the field's content $SammlungTitel = extractTitleAfterYear($child->sammelband_text); $escapedTitle = $sanitizer->selectorValue($SammlungTitel); $matchingPages = $pages->find("template=publikation, include=hidden, title^=$escapedTitle"); if ($matchingPages->count() > 0) { // die erste gefundene Seite (falls es mehrere geben sollte) $matchingPage = $matchingPages->first(); // Setze den Verweis im Feld "sammelband_verweis" $page->of(false); $page->sammelband_verweis = $matchingPage; // Debugging nach Zuweisung //bd($page->sammelband_verweis->id, 'Sammelband Verweis ID'); $page->save('sammelband_verweis'); echo '<hr>'; } } the configuration of the page reference field (auto generated by rockmigrations): 'sammelband_verweis' => [ 'addable' => '', 'allowUnpub' => 1, 'collapsed' => 0, 'columnWidth' => 100, 'derefAsPage' => 2, 'distinctAutojoin' => true, 'findPagesSelect' => '', 'findPagesSelector' => '', 'flags' => 0, 'inputfield' => '_InputfieldPageAutocomplete', 'label' => 'Sammelband', 'labelFieldFormat' => '', 'labelFieldName' => 'title', 'operator' => '%=', 'parent_id' => '/publikationen/', 'required' => '', 'requiredIf' => '', 'searchFields' => 'title', 'showIf' => '', 'tags' => 'publikation', 'template_id' => 'publikation', 'template_ids' => '', 'themeBorder' => '', 'themeColor' => '', 'themeOffset' => '', 'type' => 'FieldtypePage', ] any ideas would be highly appreceated Link to comment Share on other sites More sharing options...
da² Posted August 19 Share Posted August 19 (edited) Hi, If your page reference field can contain multiple pages, you should add pages like this: $page->sammelband_verweis->add($matchingPage); Also, for optimization, you should save the page outside of the loop: $of = $page->of(false); foreach ($page->children("include=hidden") as $child) { if (! empty($child->sammelband_text)){ // doing some sanitation to the field's content $SammlungTitel = extractTitleAfterYear($child->sammelband_text); $escapedTitle = $sanitizer->selectorValue($SammlungTitel); $matchingPages = $pages->find("template=publikation, include=hidden, title^=$escapedTitle"); if ($matchingPages->count() > 0) { $matchingPage = $matchingPages->first(); $page->sammelband_verweis->add($matchingPage); } } } $page->save('sammelband_verweis'); $page->of($of); Edited August 19 by da² Link to comment Share on other sites More sharing options...
herr rilke Posted August 20 Author Share Posted August 20 thank you @da² for the code optimization! but why it was not saving correctly was an issue with the context foreach ($page->children("include=hidden") as $child) { ... } in that case it must be $child->of(false); $child->sammelband_verweis = $matchingPage->id; //$newArray; $child->save('sammelband_verweis'); (instead of $page) ! problem solved (the only problem was kind of blind in front of the screen) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now